VBA Loop Protect sheets with password only works on first 4 sheets

Roy_Excel_Island_Apps

Board Regular
Joined
Oct 9, 2018
Messages
68
Office Version
  1. 365
Platform
  1. Windows
Hi guys!

So I have this code to loop through all sheets and protect them with a public const "pw":

Public Sub ProtectStructure()

ThisWorkbook.Protect pw, True, False

Dim sht As Worksheet

For Each sht In ThisWorkbook.Worksheets
Debug.Print sht.Name
sht.Protect pw, , , , True, , , , , , , , , True, True, True

Next sht

End Sub

After running this code all sheets are protected, but only the first 4 sheets are protected WITH the password!? What's the issue here? Workbook corrupt? I can't find a reason why the code would stop using the password in the protection after using is 4 times... Anyone who has an idea?

Regards, Roy
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
That works for me, do you have any other code that could be unprotecting some sheets?
 
Upvote 0
That works for me, do you have any other code that could be unprotecting some sheets?
Hi Fluff, good way of thinking, but no other code in there that could relate.
I found a solution in meantime:

  1. Go to the sheets that are protected without password.
  2. Unprotect it (no pop-up with password-this shows up because this is the problem).
  3. Protect is manually with a password.
  4. Do the same for all sheets.
  5. Run the code again.
  6. You'll see that the sheets are protected with the password from the macro!
Protecting them manually with a password seems to bring back the possibility to protect the sheet with a password via the macro. Magic!
 
Upvote 0
Glad you sorted it & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,584
Messages
6,120,385
Members
448,956
Latest member
JPav

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