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
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
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.