I am trying to protect sheets (visible only) in my Excel workbook in VBA. I want to allow users to edit objects on “Sheet 2” and “Sheet 3”, but not on the others.
This is the code I’m using.
Public Sub WBOpen()
Dim sh As Worksheet
Dim allowObjects As Boolean
For Each sh In Sheets
If sh.Visible = xlSheetVisible Then
**If sh.Name = "Sheet 2" Or "Sheet 3" Then**
allowObjects = True
Else
allowObjects = False
End If
sh.Protect Password:=pw(sh), DrawingObjects:=allowObjects, Contents:=True, Scenarios:=True, AllowFormattingRows:=True, AllowFiltering:=True, UserInterfaceOnly:=True
End If
Next
End Sub
I’m getting a type mismatch error on the line I’ve wrapped with **. Can anyone tell me where I’m going wrong?
Thank you
That line should be:
The type mismatch error comes because you are treating a string (
"Sheet 3") as a boolean (True/False).