Question Locked

Return the sheets selected in a workbook

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
I have an application where I ask the user to select 2 worksheets in Excel, then start a Macro. I need to determine which sheets have been selected in order to run code to perform a comparison between the 2 sheets. Does anybody have a good idea how to return the sheets which are active? Or a better way to go about this task? Thank you

3 replies

by thumbsup2

The following is from the built-in VB HELP:

[i]The SelectedSheets Property - Returns a Sheets collection that represents all the selected sheets in the specified window.

Example - This example displays a message if Sheet1 is selected in Book1.xls.

For Each sh In Workbooks("BOOK1.XLS").Windows(1).SelectedSheets
If sh.Name = "Sheet1" Then
MsgBox "Sheet1 is selected"
Exit For
End If
Next[/i]

Now, you can do the selection of multiple sheets using an array like this:

[i]Sub Several()
Worksheets(Array("Sheet1", "Sheet2", "Sheet4")).Select
End Sub[/i]

So, you should be able to figure out how to combine the SelectedSheets property with an array to determine which ones are actually selected.

If you need more help, post back and we'll dig further through the VB HELP with you.

---

by valcourtd

Sorry to jump in for a question of my own, but it is somewhat along the lines of what is being discussed here. Is there any way to create a dynamic array of sheets such that I would be able to reference them as: iArray(n) where n is the sheet's place in the array and iArray is the array of sheets?

Thanks

This discussion is closed to new replies.