How to reference multiple selected worksheets in VBA?

joeu2004

Banned user
Joined
Mar 2, 2014
Messages
3,080
Office Version
  1. 2010
Platform
  1. Windows
In Excel, I can use ctrl+click on the worksheet tabs to select multiple worksheets.

How can I refer to the collection of selected worksheets?

I would like to write:

For Each ws In Worksheets.Selection
[....]
Next

Of course, that does not work.

Alternatively, if I cycle through the collection of Worksheets, how can I detect a selected worksheet?

I would like to write:

For Each ws In Worksheets
If ws.Selected Then
[....]
EndIf
Next

Of course, that does not work.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
How about
Code:
Sub joeu2004()
   Dim ws As Worksheet
   
   For Each ws In ThisWorkbook.Windows(1).SelectedSheets
      Debug.Print ws.Name
   Next ws
End Sub
 
Upvote 0
Solution
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,216,153
Messages
6,129,179
Members
449,491
Latest member
maxim_sivakon

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