VBA Run macros on multiple sheets, not all sheets

brookementer

New Member
Joined
Jun 3, 2019
Messages
2
Hello, I have a button running three macros. I was to have this run through multiple sheets in a workbook, but not all the sheets. Just selected sheets. I have this code on the button. It seems to run through and select the sheets as the message box pops up. But it is not actually running the macros on the non-active sheets. (please excuse me if I am not clear, this is my first post to this forum). Any suggestions would be so appreciated. Thank you, Brooke

Sub Button1_Click()
Dim WkSheets As Variant, SheetName As Variant, ws As Worksheet

'** SET The Sheet Names - MUST Reflect Each Sheet Name Exactly!
WkSheets = Array("ATC1", "BEC1", "DKC1")

For Each SheetName In WkSheets

MsgBox SheetName

For Each ws In ActiveWorkbook.Worksheets
If ws.Name = SheetName Then

Call CheckGoalSeekRev 'Macro1
Call CheckGoalSeekCos 'Macro2
Call gsMultiCols 'Macro3


End If
Next
Next
End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hi & welcome to MrExcel.
Try
Code:
Sub Button1_Click()
   Dim ws As Worksheet
   
   '** SET The Sheet Names - MUST Reflect Each Sheet Name Exactly!
   WkSheets = Array("ATC1", "BEC1", "DKC1")
   
   For Each ws In Sheets(Array("ATC1", "BEC1", "DKC1"))
      ws.Select
      Call CheckGoalSeekRev 'Macro1
      Call CheckGoalSeekCos 'Macro2
      Call gsMultiCols 'Macro3
   Next ws
End Sub
 
Upvote 0
Welcome to the Board!

That is because looping through the sheets does not actually select/activate them.
Add the following line before your first Macro Call:
Code:
ws.Activate
 
Upvote 0
You are welcome.
Glad we were able to help.
 
Upvote 0

Forum statistics

Threads
1,214,409
Messages
6,119,339
Members
448,888
Latest member
Arle8907

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