Run The Same Macro On Some Worksheets of my Workbook

coscongr

New Member
Joined
Jan 28, 2018
Messages
13
Hello,

after some failures I had I would like to have your help.
I want to run a macro which runs fine when I run it manually but I want a button to do the same sums in multiple sheets.
My workbook has many sheets and I want to run this macro in 4 sheets let say "aaaa_30", "bbbb_31", "cccc_32", "dddd_33"

Code:
[COLOR=#26282A][FONT='inherit']Sub Sheets_for_autosum()[/FONT][/COLOR]
[COLOR=#26282A][FONT='inherit']
[/FONT][/COLOR]
[COLOR=#26282A][FONT='inherit']Sheets(Array("aaaa_430", "bbbb_431", "cccc_432", "dddd_433")).Select Sheets("aaaa_430", "bbbb_431", "cccc_432", "dddd_433").
Activate Application.Run "myfile.xlsm!AUTOSUMS"[/FONT][/COLOR]
[COLOR=#26282A][FONT='inherit']
End Sub[/FONT][/COLOR]

Thank you
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Code:
Sub Sheets_for_autosum()
Dim Ary As Variant, i As Long
Ary = Array("aaaa_430", "bbbb_431", "cccc_432", "dddd_433")
    For i = LBound(Ary) To UBound(Ary)
        Sheets(Ary(i)).Activate
        Application.Run "myfile.xlsm!AUTOSUMS"
    Next
End Sub

This assumes that AUTOSUMS is written to run on any active sheet.
 
Last edited:
Upvote 0
Code:
Sub Sheets_for_autosum()
Dim Ary As Variant, i As Long
Ary = Array("aaaa_430", "bbbb_431", "cccc_432", "dddd_433")
    For i = LBound(Ary) To UBound(Ary)
        Sheets(Ary(i)).Activate
        Application.Run "myfile.xlsm!AUTOSUMS"
    Next
End Sub

This assumes that AUTOSUMS is written to run on any active sheet.

How can I change this? I want to run it only in four sheets!
 
Upvote 0
Replace the sheet names in the array.
Code:
Ary = Array[COLOR=#ff0000]("aaaa_430", "bbbb_431", "cccc_432", "dddd_433")[/COLOR]

I don't know what the called macro does, so it could already address more than four sheets. You did not poste it for us to analyze.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,895
Members
449,097
Latest member
dbomb1414

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