Discover names of open Workbooks/Worksheets

C

Chris Watts

I am trying to discover the names of the Workbooks and Worksheets that are
open (not active) in Excel, using vba.

When I try:

Sub DiscoverOpenSheets()
Dim wbBook As Workbook
Dim wsSheet As Worksheet

For Each wbBook In Application
MsgBox wbBook.Name
Next

For Each wsSheet In ActiveWorkbook
MsgBox wsSheet.Name
Next

End Sub

Each of the "For Each .. In .." statements causes an error - "Object does
not support this property or method".
What am I doing wron?

TIA
Chris
 
D

Dave Peterson

Sub DiscoverOpenSheets()
Dim wbBook As Workbook
Dim wsSheet As Worksheet

For Each wbBook In Application.workbooks
MsgBox wbBook.Name
Next wbBook

For Each wsSheet In ActiveWorkbook.worksheets
MsgBox wsSheet.Name
Next wsSheet

End Sub
 
D

Dave Peterson

And you may want:

Sub DiscoverOpenSheets()
Dim wbBook As Workbook
Dim wsSheet As Worksheet

For Each wbBook In Application.workbooks
For Each wsSheet In wbbook.worksheets
MsgBox wsSheet.Name
Next wsSheet
next wbbook

End Sub
 
D

Don Guillett

Nice. To make a list try this small mod

Sub DiscoverOpenSheets()
Dim wbBook As Workbook
Dim wsSheet As Worksheet
Dim r As Long
r = 1
For Each wbBook In Application.Workbooks
'MsgBox wbBook.Name
For Each wsSheet In wbBook.Worksheets
Cells(r, 1) = wbBook.Name
Cells(r, 2) = wsSheet.Name
'MsgBox wsSheet.Name
r = r + 1
Next wsSheet
Next wbBook
columns("a:b").autofit
End Sub
 
D

Dave Peterson

And just in case any of those names look like numbers (or dates)...

Cells(r, 1) = "'" & wbBook.Name
Cells(r, 2) = "'" & wsSheet.Name




Don said:
Nice. To make a list try this small mod

Sub DiscoverOpenSheets()
Dim wbBook As Workbook
Dim wsSheet As Worksheet
Dim r As Long
r = 1
For Each wbBook In Application.Workbooks
'MsgBox wbBook.Name
For Each wsSheet In wbBook.Worksheets
Cells(r, 1) = wbBook.Name
Cells(r, 2) = wsSheet.Name
'MsgBox wsSheet.Name
r = r + 1
Next wsSheet
Next wbBook
columns("a:b").autofit
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top