I want to write a macro, that in all sheets locks certain cells -- from A12 to last row of R. Thing is, that I get
error 1004: "Method 'Range' Of Object '_Worksheet' failed"
in line
LastRow = wSheet.Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row)
.
Could anyone help me out? Thanks!
Option Explicit
Sub ProtectAll()
Dim wSheet As Worksheet
Dim Pwd As String
Dim LastRow As Integer
Pwd = InputBox("Enter your password to protect all worksheets", "Password Input")
For Each wSheet In Worksheets
LastRow = wSheet.Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
wSheet.Range(Cells(12, 1), Cells(LastRow, 18)).Select
wSheet.Protect Password:=Pwd, AllowFiltering:=True
Next wSheet
End Sub
Your code will fail if the sheet is blank as it currently assumes that it finds at least one non blank cell when it sets
LastRow
.Try using a range object instead, test that it is
Not Nothing
before usingLastRow
.Updated: for completeness added a check to see if sheets were already protected, and if so skips and otes these