list the sheets in a workbook

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need to be able to list the names of the sheets contained in a workbook.
 
here is a vb.net example that should work for ya.

Dim sheet As Excel.Worksheet
Dim ODOC As Excel.Workbook

................

Dim worksheetname As String
For Each sheet In ODOC.Worksheets
Console.WriteLine(sheet.Name)
Next
 
Function ListSheets() As String
For Each sht In ActiveWorkbook.Sheets
ListSheets = ListSheets & "," & sht.Name
Next
End Function

Sub aa()
MsgBox Mid(ListSheets, 2)
End Sub

Run the sub aa to get a comma delimited string containing the names of the
sheets. If you want to include worksheets only, replace ActiveWorkbook.Sheets
by ActiveWorkbook.Worksheets.
 
Dim ODOC As Excel.Workbook
Dim sheet As Excel.Worksheet

.......
Dim worksheetname As String
For Each sheet In ODOC.Worksheets
Console.WriteLine(sheet.Name)
Next
 
sStr = ""
for each sh in Activeworkbook.sheets
sStr = sStr & sh.Name & vbNewLine
Next
msgBox sStr

--
Regards,
Tom Ogilvy

"Listing of Excel shhets in a workbook" <Listing of Excel shhets in a
(e-mail address removed)> wrote in message
news:[email protected]...
 
Listing said:
I need to be able to list the names of the sheets contained in a workbook.
Here are a couple of ways:

Sub qwerty4()
'Workbook must have reference to Microsoft Scripting Runtime
Dim dic As Dictionary, rng As Range
Set dic = New Dictionary
Set rng = Range("A1:A" & ActiveWorkbook.Sheets.Count)
For Each sh In ActiveWorkbook.Sheets
dic.Add CStr(sh.Name), sh.Name
Next
rng.Value = Application.Transpose(dic.Items)
End Sub

Sub qwerty5()
Dim arr, rng As Range
Dim i As Integer, n As Integer, m As Object
Set m = ActiveWorkbook.Sheets
n = m.Count
Set rng = Range("A1:A" & n)
ReDim arr(1 To n, 1 To 1)
i = 1
For Each sh In m
arr(i, 1) = sh.Name
i = i + 1
Next
rng.Value = arr
End Sub

Alan Beban
 

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

Back
Top