Loop through Selected worksheets

JC2710

Board Regular
Joined
Mar 10, 2008
Messages
164
Hi

Can anyone tell me how to loop through only Selected / Grouped worksheets in workbook? I have the code below but cant find a selected property in Sheet.

Code:
 For Each Sheet In ThisWorkbook.Sheets
        
                    MsgBox Sheet.Name
       
          Next

Thanks
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
You need to use the Window object.

Straight from the help file:

Code:
For Each sh In Workbooks("BOOK1.XLS").Windows(1).SelectedSheets
    If sh.Name = "Sheet1" Then
        MsgBox "Sheet1 is selected"
        Exit For
    End If
Next
 
Upvote 0
Thanks Chris

Works great! Seems a strange way to have to do it though. I would have thought that each sheet in the workbook would have a selected property!

Anyway. Thanks Again
 
Upvote 0
Hi JC2710

I would have thought that each sheet in the workbook would have a selected property!

Sorry, but that does not agree with the excel object model.

The fact that a worksheet is selected cannot be reflected directly in the worksheet or in the workbook. As you know a workbook can be displayed in several Windows and in each of the Windows you may select a different group of worksheets. That's why this in a Windows property. In Window 1 you can have Sheet1 and Sheet3 grouped, but in Window 2 of the same Workbook you may have only Sheet5 selected. It makes no sense to ask if Sheet1 is selected unless you specify in which window.

You can also use the ActiveWindow:

Code:
Dim wsh As Worksheet
 
For Each wsh In ActiveWindow.SelectedSheets
    MsgBox wsh.Name
Next wsh
 
Last edited:
Upvote 0

Forum statistics

Threads
1,202,987
Messages
6,052,937
Members
444,616
Latest member
novit19089

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