Protecting Multiple Sheets in a Workbook at One Time

Arianava

New Member
Joined
Aug 8, 2011
Messages
6
Hi, there!!

When searching the internet for an answer as to how I could protect all sheets in my workbook at one time, instead of having to protect each one individually, the only thing I found that would actually be a perfect fit was creating a macro.

I've never used a macro before, and know absolutely nothing about how to create one!

Given that, I attempted the following code that I found in another forum:

Sub Protect_Me()
Dim x As Long
For x = 1 To Worksheets.Count
If Sheets(x).ProtectContents Then
Sheets(x).Unprotect Password:="MyPass"
Else
Sheets(x).Protect Password:="MyPass"
End If
Next
End Sub
When I tried to run it in Excel 2007, I got a Sytax Error, which shows the first line (Sub Protect_Me()) highlighted in yellow, with a little yellow arrow pointing to it. I'm assuming the program doesn't like this line. I couldn't tell you why!

Can someone tell me why, and tell me how to correct it so that it works?

Thanks bunches!!

Ariana
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
This code works fine for me, also Excel 2007.

Where did you paste that code?
 
Upvote 0
try this code will protect all sheets with PassWord "MyPass" change it as you want
Code:
Sub ProSheets()
Dim ws As Worksheet
For Each ws In Sheets
    ws.Protect "MyPass"
Next ws
End Sub
 
Upvote 0
try this code will protect all sheets with PassWord "MyPass" change it as you want
Code:
Sub ProSheets()
Dim ws As Worksheet
For Each ws In Sheets
    ws.Protect "MyPass"
Next ws
End Sub
Awesome, works fabulous! However, how would I toggle this so it would unprotect all the sheets at once with the password?
 
Upvote 0
Awesome, works fabulous! However, how would I toggle this so it would unprotect all the sheets at once with the password?

just change word Protect to UnProtect
Code:
Sub ProSheets()
Dim ws As Worksheet
For Each ws In Sheets
    ws.UnProtect "MyPass"
Next ws
End Sub
 
Upvote 0
Yahya,

Sorry for the delay in responding - I haven't had a chance to play with the macro until now. I did get your code to work! Thanks! Now, I just need to learn how to navigate macros properly!

Thanks bunches!
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,710
Members
452,939
Latest member
WCrawford

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