Protect sheets with macro

kanadaaa

Active Member
Joined
Dec 29, 2019
Messages
348
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.
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
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
 
Upvote 0
Sorry again, it seems like I had the End If row in a wrong place. Thank you ?
 
Upvote 0

Forum statistics

Threads
1,214,813
Messages
6,121,706
Members
449,048
Latest member
81jamesacct

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
Back
Top