daisypetals313
asked on
Assigning Macro to Multiple Worksheets
I am trying to do a macro to affect multiple worksheets. I have the code below and want to add for worksheets called Master2 and Master3. Where should I add these?
Private Sub Workbook_Open()
With Worksheets("master")
.EnableOutlining = True
.Protect UserInterfaceOnly:=True
End With
End Sub
Private Sub Workbook_Open()
With Worksheets("master")
.EnableOutlining = True
.Protect UserInterfaceOnly:=True
End With
End Sub
ASKER
It does something strange....when I open the workbook it's asks me for the password to unprotect my sheet. How can I get it to not do this?
Put .Unprotect "SomePassword" between lines 7 and 8. And BTW you can do this to protect the sheets.
.Protect Password:="SomePassword", Userinterfaceonly:=True
.Protect Password:="SomePassword", Userinterfaceonly:=True
ASKER
I did the below. It was successful in removing the password prompt upon open, however, now the group/ungroup function doesn't work and is saying it can't be done a protected sheet.
Private Sub Workbook_Open()
Dim sht As Worksheet
For Each sht In Worksheets
With sht
If sht.Name = "master" or sht.Name = "Master2" or sht.Name = "Master3" Then
.EnableOutlining = True
.Unprotect "SomePassword"
.Protect UserInterfaceOnly:=True
End If
End With
Next
End Sub
Private Sub Workbook_Open()
Dim sht As Worksheet
For Each sht In Worksheets
With sht
If sht.Name = "master" or sht.Name = "Master2" or sht.Name = "Master3" Then
.EnableOutlining = True
.Unprotect "SomePassword"
.Protect UserInterfaceOnly:=True
End If
End With
Next
End Sub
There are situations where you need to Unprotect just before the code an Protect right after. But you can also look at the options in the protect sheet dropdown and see if any apply to what you are doing
ASKER
I did and none apply to this situation. Do you know how to address my question, or maybe better if I close this out and ask again with a sample file and allow others to attempt the solution?
When you mention "group/ungroup function" are you talking about VBA code? If so then do Unprotect just before the code and Protect right after.
ASKER
The group/ungroup I am referencing is the one that is accessed via Data | Group (to setup) and then to view it you would click the +/- at the top of the worksheet. With this explanation do I still put the password code before & after like this? Also, if you reply with a correction to the code, could you kindly type it into the code rather than verbally describing where it should go as I am new at this and may not understand. Thanks.
.Unprotect "SomePassword"
Dim sht As Worksheet
For Each sht In Worksheets
With sht
If sht.Name = "master" or sht.Name = "Master2" or sht.Name = "Master3" Then
.EnableOutlining = True
.Protect UserInterfaceOnly:=True
End If
End With
Next
End Sub
.Protect Password:="SomePassword", Userinterfaceonly:=True
.Unprotect "SomePassword"
Dim sht As Worksheet
For Each sht In Worksheets
With sht
If sht.Name = "master" or sht.Name = "Master2" or sht.Name = "Master3" Then
.EnableOutlining = True
.Protect UserInterfaceOnly:=True
End If
End With
Next
End Sub
.Protect Password:="SomePassword", Userinterfaceonly:=True
Try this.
Dim sht As Worksheet
For Each sht In Worksheets
With sht
If sht.Name = "master" or sht.Name = "Master2" or sht.Name = "Master3" Then
' It's one of the sheets I want to modify so..
' first unprotect it...
.Unprotect "SomePassword"
' then do this (which seems not to do much of anything)...
.EnableOutlining = True
' We're done so protect the sheet again
.Protect Password:="SomePassword", Userinterfaceonly:=True
End If
End With
Next
End Sub
If you're less comfortable with VBA and want to keep it as simple as possible (you fit a majority of the people I work with!), you can simply replicate your code for the other sheets as I've done below. The answer above is perfect, and is more scalable for larger applications... but if you're dealing with small worksheets and only need simple code, sometimes it -is- easier to keep it simple.
Again, the answers above are perfect. Feel free to accept those. Just note that if you're the type of person that's comfortable recording macros and you're dealing with smaller worksheets and smaller changes you can copy/paste to your heart's content. Something like this would not be noticeably slower when performed this way and certainly wouldn't make your code too bulky. :)
Again, the answers above are perfect. Feel free to accept those. Just note that if you're the type of person that's comfortable recording macros and you're dealing with smaller worksheets and smaller changes you can copy/paste to your heart's content. Something like this would not be noticeably slower when performed this way and certainly wouldn't make your code too bulky. :)
Private Sub Workbook_Open()
With Worksheets("master")
.EnableOutlining = True
.Protect UserInterfaceOnly:=True
End With
With Worksheets("master2")
.EnableOutlining = True
.Protect UserInterfaceOnly:=True
End With
With Worksheets("master3")
.EnableOutlining = True
.Protect UserInterfaceOnly:=True
End With
End Sub
ASKER
I copied the above and password prompt still appears. I attached a sample file. Password is "test".
Book1.xlsm
Book1.xlsm
The code in that workbook uses:
.Protect
Which is the code to protect your workbook with a password.
Replace the code with the following and try again:
.Protect
Which is the code to protect your workbook with a password.
Replace the code with the following and try again:
Private Sub Workbook_Open()
With Worksheets("sheet1")
.EnableOutlining = True
.Unprotect Password:="test"
End With
With Worksheets("sheet2")
.EnableOutlining = True
.Unprotect Password:="test"
End With
With Worksheets("sheet3")
.EnableOutlining = True
.Unprotect Password:="test"
End With
End Sub
What happens when you try the code I posted in post ID: 39684640?
ASKER
That doesn't work either. A sample file with it is attached below. While that code is succesful in preventing the password prompt box from coming up, it still prevents the user from adjusting grouped/ungrouped columns in protected worksheets.
Book1.xlsm
Book1.xlsm
ASKER
And I just read through my original question and realize I wasn't clear about this in the beginning because I thought that code I provided was how to do it for one sheet and thought I could just apply this to multiple sheets and be in the clear. Sorry I made this so confusing...I wasn't of clear head last night after lots of turkey and wine
That solution seems to work fine, but you may be attempting to group/ungroup on either "master2" or "master3" -- neither of which are set up with the code. "Master" seems to work.
Test again and make sure you're on the "master" sheet.
Likewise, you can update your code to the following to change all sheets.
Test again and make sure you're on the "master" sheet.
Likewise, you can update your code to the following to change all sheets.
Private Sub Workbook_Open()
With Worksheets("master")
.Unprotect
.EnableOutlining = True
.Protect UserInterfaceOnly:=True
End With
With Worksheets("master2")
.Unprotect
.EnableOutlining = True
.Protect UserInterfaceOnly:=True
End With
With Worksheets("master3")
.Unprotect
.EnableOutlining = True
.Protect UserInterfaceOnly:=True
End With
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This is it, thanks! I modified slightly to the below for use with one sheet as well:
Private Sub Workbook_Open()
Dim sht As Worksheet
For Each sht In Worksheets
With sht
If sht.Name = "MASTER" Then
.Unprotect "test"
.EnableOutlining = True
.Protect Userinterfaceonly:=True
End If
End With
Next
End Sub
Private Sub Workbook_Open()
Dim sht As Worksheet
For Each sht In Worksheets
With sht
If sht.Name = "MASTER" Then
.Unprotect "test"
.EnableOutlining = True
.Protect Userinterfaceonly:=True
End If
End With
Next
End Sub
You're welcome and I'm glad I was able to help.
Marty - MVP 2009 to 2013
Marty - MVP 2009 to 2013
Open in new window