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
daisypetals313Asked:
Who is Participating?
 
Martin LissConnect With a Mentor Older than dirtCommented:
The only reason my solution didn't work is that you said the other names were "Master2" and "Master3" (capitalized) and my code used lower case.
I've attached a working solution.
Q-28306250.xlsm
0
 
Martin LissOlder than dirtCommented:
Private Sub Workbook_Open()

Dim sht As Worksheet
    For Each sht in Worksheets
     With sht
        If sht.Nsme = "master" or sht.Name = "Master2" or sht.Name = "Master3" Then
         .EnableOutlining = True
         .Protect UserInterfaceOnly:=True
       End If
     End With
    Next
 End Sub

Open in new window

0
 
daisypetals313Author Commented:
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?
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
Martin LissOlder than dirtCommented:
Put .Unprotect "SomePassword" between lines 7 and 8. And BTW you can do this to protect the sheets.

.Protect Password:="SomePassword", Userinterfaceonly:=True
0
 
daisypetals313Author Commented:
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
0
 
Martin LissOlder than dirtCommented:
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
0
 
daisypetals313Author Commented:
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?
0
 
Martin LissOlder than dirtCommented:
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.
0
 
daisypetals313Author Commented:
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
0
 
Martin LissOlder than dirtCommented:
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 

Open in new window

0
 
SeanStricklandCommented:
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.  :)

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

Open in new window

0
 
daisypetals313Author Commented:
I copied the above and password prompt still appears.  I attached a sample file.  Password is "test".
Book1.xlsm
0
 
SeanStricklandCommented:
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:

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

Open in new window

0
 
Martin LissOlder than dirtCommented:
What happens when you try the code I posted in post ID: 39684640?
0
 
daisypetals313Author Commented:
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
0
 
daisypetals313Author Commented:
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
0
 
SeanStricklandCommented:
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.

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

Open in new window

0
 
daisypetals313Author Commented:
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
0
 
Martin LissOlder than dirtCommented:
You're welcome and I'm glad I was able to help.

Marty - MVP 2009 to 2013
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.