Solved

Assigning Macro to Multiple Worksheets

Posted on 2013-11-28
19
169 Views
Last Modified: 2013-11-30
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
0
Comment
Question by:daisypetals313
  • 8
  • 8
  • 3
19 Comments
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
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
 

Author Comment

by:daisypetals313
Comment Utility
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
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
Put .Unprotect "SomePassword" between lines 7 and 8. And BTW you can do this to protect the sheets.

.Protect Password:="SomePassword", Userinterfaceonly:=True
0
 

Author Comment

by:daisypetals313
Comment Utility
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
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
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
 

Author Comment

by:daisypetals313
Comment Utility
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
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
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
 

Author Comment

by:daisypetals313
Comment Utility
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
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 11

Expert Comment

by:SeanStrickland
Comment Utility
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
 

Author Comment

by:daisypetals313
Comment Utility
I copied the above and password prompt still appears.  I attached a sample file.  Password is "test".
Book1.xlsm
0
 
LVL 11

Expert Comment

by:SeanStrickland
Comment Utility
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
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
What happens when you try the code I posted in post ID: 39684640?
0
 

Author Comment

by:daisypetals313
Comment Utility
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
 

Author Comment

by:daisypetals313
Comment Utility
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
 
LVL 11

Expert Comment

by:SeanStrickland
Comment Utility
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
 
LVL 45

Accepted Solution

by:
Martin Liss earned 500 total points
Comment Utility
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
 

Author Closing Comment

by:daisypetals313
Comment Utility
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
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
You're welcome and I'm glad I was able to help.

Marty - MVP 2009 to 2013
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

What is a Form List Box? (skip if you know this) The forms List Box is the alternative to the ActiveX list box. If you are using excel 2007, you first make sure you have a developer tab (click the Orb)->"Excel Options"->Popular->"Show Developer tab…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now