VBA: looping through an array of tabs

JaredSorensen

Board Regular
Joined
Aug 23, 2007
Messages
160
I have a workbook that I want to run a macro on for each of the selected tabs. Could you please advise me regarding how I can cycle through multiple tabs that are selected prior to initiating the macro? I have the code down for what I need to do on each tab, but need this outer piece.

Please assume I have a tabs Sheet1, Sheet2, and Sheet3 selected.

This is the code I see when I select them all:
Code:
Sheets(Array("renamed", "Sheet1", "Sheet2", "Sheet3")).Select

This is the generic code for advancing to the next tab; I would prefer something more sophisticated than this--even a formal loop.
Code:
    ActiveSheet.Previous.Select

Ideally, I'd like to use the For Each . . . Next construct, if someone knows how that could be used. Thank you very much!

Jared
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.

jindon

MrExcel MVP
Joined
Aug 21, 2004
Messages
16,995
Does this help?
Code:
Sub test()
Dim ws As Worksheet
For Each ws In ActiveWindow.SelectedSheets
    MsgBox ws.name
Next
End Sub
 
Upvote 0

JaredSorensen

Board Regular
Joined
Aug 23, 2007
Messages
160
jindon,

It works perfectly! That was the kind of elegant solution I was looking for.

Thank you and have a great evening.

Jared
 
Upvote 0

Forum statistics

Threads
1,190,898
Messages
5,983,467
Members
439,843
Latest member
PlanetFitness

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
Top