Protect sheets with macro

kanadaaa

Active Member
Joined
Dec 29, 2019
Messages
312
Office Version
  1. 365
Platform
  1. Windows
Hi, I was looking for a macro code that protects all the worksheets on the active workbook and got to the following code:
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
This works well, but I want one worksheet to be excluded from this macro.
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
This returns "Compile error: Next without for".
Do you have any idea what is the problem? Thank you.
 

Some videos you may like

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.

CsJHUN

Active Member
Joined
Jan 13, 2015
Messages
357
Office Version
  1. 2016
Platform
  1. Windows
Hi, missing an "end if"
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
End If   '<-----------------------------------
Next wSheet
End Sub
 

kanadaaa

Active Member
Joined
Dec 29, 2019
Messages
312
Office Version
  1. 365
Platform
  1. Windows
Sorry again, it seems like I had the End If row in a wrong place. Thank you 😂
 

Watch MrExcel Video

Forum statistics

Threads
1,127,606
Messages
5,625,767
Members
416,136
Latest member
senthil_sk

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top