Running a macro on all manuallly selected sheets

mattxu2006

New Member
Joined
Mar 25, 2011
Messages
37
I have a formatting macro that works on the active sheet. (I just go to the worksheet that I want to format and then run the macro) I would like to be able to select multiple sheets in a workbook and then have the macro work on all selected sheets.
I don't know how many sheets may be in the workbook and I do not know to which sheets the macro will be applied until selecting them manually.
Thanks for any guidance
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Here's a 'very' basic example, but implimenting this to your existing code depends on what that code looks like.
ws becomes an object variable representing each sheet you have selected.
Any commands you have manipulating cells in your existing code, you add ws. to it, as below.

Rich (BB code):
Dim ws As Worksheet
For Each ws In ActiveWindow.SelectedSheets
    ws.Range("A1").Value = "Hello"
Next ws
 
Last edited:
Upvote 0
Depending on what your formatting code looks like you could do something like this:
Code:
Dim sht As Worksheet
Sub AllSelectedSheets()
Application.ScreenUpdating = False
For Each sht In ActiveWindow.SelectedSheets
    Call myMacro
Next sht
Application.ScreenUpdating = True
End Sub
Sub myMacro()
With sht 'your desired formatting here
    .UsedRange.Columns.ColumnWidth = 20
    .UsedRange.Cells.Font.Size = 14
End With
End Sub

[/code]
 
Upvote 0
sorry for the slow response.
For the most part, this is the answer. Thank you.

However, I am running into an issue with trying to apply protection to the worksheet.

Code:
Dim wS As Worksheet
Dim myPassword As String
    
myPassword = "" 'Remove password (i.e. make "") to have no password
For Each wS In ActiveWindow.SelectedSheets
With wS
    
    .Protect Password:=myPassword, _
    Contents:=True, _
    AllowFormattingCells:=True, _
    AllowFormattingColumns:=True, _
    AllowFormattingRows:=True, _
    AllowInsertingColumns:=True, _
    AllowInsertingRows:=True, _
    AllowInsertingHyperlinks:=True, _
    AllowDeletingColumns:=True, _
    AllowDeletingRows:=True, _
    AllowSorting:=True, _
    AllowFiltering:=True, _
    AllowUsingPivotTables:=True, _
    DrawingObjects:=True, _
    Scenarios:=False, _
    UserInterfaceOnly:=True 'Note—this statement and the next are necessary to allow grouping.
    .EnableOutlining = True
    .EnableSelection = xlNoRestrictions 'Set to xlNoRestrictions or xlUnlockedCells or xlNoSelection
    End With
Next wS


Error Code 1004
Method 'Protect' of '_worksheet' failed

I know that is an issue with identifying the correct object, but that is as far as I understand.
 
Upvote 0
When you wrote this in thee
myPassword = "" 'Remove password (i.e. make "") to have no password

That to me implies that the sheet is 'Already' protected.
Therefore you must first UNprotect the sheet with the current password
THEN you can ReProtect it with "" as the password.

So the first line after With

Code:
With wS
    .UnProtect Password:="[COLOR=#FF0000]CurrentPasswordHere[/COLOR]"
    .Protect Password:=myPassword,
 
Upvote 0
When you wrote this in thee


That to me implies that the sheet is 'Already' protected.
Therefore you must first UNprotect the sheet with the current password
THEN you can ReProtect it with "" as the password.

So the first line after With

Code:
With wS
    .UnProtect Password:="[COLOR=#ff0000]CurrentPasswordHere[/COLOR]"
    .Protect Password:=myPassword,

That piece of text should not be consequential as it has ' in front. it is meant as a marker. currently the password is = "" and this is a place to change that.

the issue appears to be that the code does not see the worksheet as an object
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,198
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top