Solved

Assigning Macro to Multiple Worksheets

Posted on 2013-11-28
19
212 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 8
  • 3
19 Comments
 
LVL 47

Expert Comment

by:Martin Liss
ID: 39684299
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
ID: 39684376
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 47

Expert Comment

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

.Protect Password:="SomePassword", Userinterfaceonly:=True
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:daisypetals313
ID: 39684411
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 47

Expert Comment

by:Martin Liss
ID: 39684417
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
ID: 39684456
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 47

Expert Comment

by:Martin Liss
ID: 39684469
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
ID: 39684535
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 47

Expert Comment

by:Martin Liss
ID: 39684640
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
 
LVL 11

Expert Comment

by:SeanStrickland
ID: 39684715
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
ID: 39684721
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
ID: 39684724
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 47

Expert Comment

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

Author Comment

by:daisypetals313
ID: 39685704
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
ID: 39685712
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
ID: 39685723
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 47

Accepted Solution

by:
Martin Liss earned 500 total points
ID: 39685759
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
ID: 39687164
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 47

Expert Comment

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

Marty - MVP 2009 to 2013
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

724 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