![]() |
![]() |
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
New Member
Join Date: Apr 2002
Posts: 17
|
Anyone have a link to some code that protects and unprotects multiple sheets in one go ? |
|
|
|
|
|
#2 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Monterrey, Mexico
Posts: 1,433
|
You cannot protect/unprotect more than one sheet at once, have you tried the following:
Worksheets("Sheet1").Protect Password:="password", DrawingObjects:=True, Contents:=True, Scenarios:=True Worksheets("Sheet1").Unprotect ("password") Worksheets("Sheet2").Protect Password:="password", DrawingObjects:=True, Contents:=True, Scenarios:=True Worksheets("Sheet2").Unprotect ("password") Edit as needed.
__________________
Kind regards, Al Chara |
|
|
|
|
|
#3 |
|
Board Regular
Join Date: Mar 2002
Location: Near the Land of Oz
Posts: 1,550
|
Actually, someone gave me the following a few weeks ago. These two macros will do it for all sheets in a workbook.
Sub ProtectAll() Dim ws As Worksheet For Each ws In ActiveWorkbook.Worksheets ws.Protect password:="whatever", DrawingObjects:=True, Contents:=True, Scenarios:=True Next ws End Sub ----------- Sub DeProtectAll() Dim ws As Worksheet For Each ws In ActiveWorkbook.Worksheets ws.Unprotect password:="whatever" Next ws End Sub NOTE: "whatever" is the password (without the quotes) you supply _________________ - old, slow, and confused ... but at least I'm inconsistent - [ This Message was edited by: shades on 2002-04-15 08:02 ] |
|
|
|
|
|
#4 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Monterrey, Mexico
Posts: 1,433
|
Shades code will work if you want to do all of the sheets of a workbook, but not if you only want to do some of the worksheets. If you want to use a for loop, then you will have to do something like the following:
Sub ProtectWS() Dim ws As Worksheet Set WSArray = Sheets(Array("Sheet1", "Sheet3")) For Each ws In WSArray ws.Protect password:="password", DrawingObjects:=True, Contents:=True, Scenarios:=True Next end sub Edit as needed.
__________________
Kind regards, Al Chara |
|
|
|
|
|
#5 |
|
New Member
Join Date: Jul 2010
Posts: 3
|
I am new to VBA code, so I don't fully understand. If I insert this into my VBA code, what sheet do I use - This Workbook or Module? Also, once I have it inserted, how do I run it? Sorry for the basic questions, but until I get training (sometime in September) I have to go this route.
|
|
|
|
|
|
#6 |
|
MrExcel MVP
Moderator New Pool Boy Join Date: Sep 2004
Location: Greyton, South Africa
Posts: 7,960
|
Hi and welcome to the board!
The code you refer to should sit within a standard module. You can either run it by hitting ALT+F8 and then choosing the macro from the dialog that opens, or you can attach the macro to a forms button in one of your sheets.
__________________
Regards, Jon von der Heyden - Posting guidelines, forum rules and terms of use - Try searching for your answer first, see how - Read the FAQs - List of BB codes - Excel Design Solutions |
|
|
|
|
|
#7 |
|
New Member
Join Date: Jul 2010
Posts: 3
|
Thank you so much for your assistance! This worked like a charm!!
|
|
|
|
|
|
#8 |
|
New Member
Join Date: Jul 2010
Posts: 3
|
I'm sorry, one last question. Can I modify this code to unprotect the worksheets as well? I tried the following:
Sub UnProtectWS() Dim ws As Worksheet Set WSArray = Sheets(Array("Sheet1","Sheet3")) For Each ws In WSArray ws.Unprotect Password:="password", DrawingObjects:=True, Contents:=True, Scenarios:=True Next End Sub However, I get a Compile Error:Named argument not found, and the "DrawingObjects:" is highlighted. What am I doing wrong? Thank you ~ Agieseke |
|
|
|
|
|
#9 |
|
MrExcel MVP
Moderator New Pool Boy Join Date: Sep 2004
Location: Greyton, South Africa
Posts: 7,960
|
Those arguments are not available when you unprotect, only when you protect. So it would be:
Code:
Sub UnProtectWS()
Dim ws As Worksheet
Set WSArray = Sheets(Array("Sheet1","Sheet3"))
For Each ws In WSArray
ws.Unprotect Password:="password"
Next
End Sub
__________________
Regards, Jon von der Heyden - Posting guidelines, forum rules and terms of use - Try searching for your answer first, see how - Read the FAQs - List of BB codes - Excel Design Solutions |
|
|
|
|
|
#10 |
|
New Member
Join Date: Sep 2010
Posts: 12
|
I am using the above macros and have been successful in protecting multiple worksheets with a password, but I can't seem to protect them in a way that allows me to continue to use the Pivot Table Reports. Is there additional language that I can include that will select only "Use Pivot Table Reports" from the list on the "Protect Sheet" screen?
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|