kanadaaa
Active Member
- Joined
- Dec 29, 2019
- Messages
- 348
- Office Version
- 365
- Platform
- Windows
Hi, I was looking for a macro code that protects all the worksheets on the active workbook and got to the following code:
This works well, but I want one worksheet to be excluded from this macro.
My naive attempt is:
This returns "Compile error: Next without for".
Do you have any idea what is the problem? Thank you.
VBA Code:
Sub ProtectAll()
Dim wSheet As Worksheet
Dim Pwd As String
Pwd = InputBox("Input password to protect all sheets", "Password")
For Each wSheet In Worksheets
wSheet.Protect Password:=Pwd, DrawingObjects:=True, Contents:=True, Scenarios:=True, _
AllowFormattingColumns:=True, AllowFormattingRows:=True
Next wSheet
End Sub
My naive attempt is:
VBA Code:
Sub ProtectAll()
Dim wSheet As Worksheet
Dim Pwd As String
Pwd = InputBox("Input password to protect all sheets", "Password")
For Each wSheet In Worksheets
If wSheet.Name = "Rates" Then
'Do Nothing
Else
wSheet.Protect Password:=Pwd, DrawingObjects:=True, Contents:=True, Scenarios:=True, _
AllowFormattingColumns:=True, AllowFormattingRows:=True
Next wSheet
End If
End Sub
Do you have any idea what is the problem? Thank you.