Help grabbing a list of Excel worksheets?

PaulSpell

Registered User.
Local time
Today, 23:37
Joined
Apr 19, 2002
Messages
201
Can anyone tell me how to grab a list of worksheets from an Excel workbook?

I am writing some import functions that use mutli sheet workbooks as a data source and need to create a list of the worksheets in a workbook, so that I can use this list to populate a list box and ask the user which sheet to import (same sort of thing Access does when you import a sheet from a workbook).
 
Look into using Excel Automation from VBA. You can Iterate through the WorkSheets and grab there names.
 
That is what I mean, but how do you do it?
 
Code Provided is based off information from the following Microsoft Knowledge base article
142996

Code:
Public Sub ListExcelWorkSheets()
Dim i As Integer
Dim XL As Object
Dim WrkBook As Object
    Set XL = CreateObject("Excel.Application")
    Set WrkBook = XL.Workbooks.Open("C:\Temp\Book1.xls")
    For i = 1 To WrkBook.sheets.Count
       Debug.Print WrkBook.sheets(i).Name
    Next i%
    XL.Quit
    Set WrkBook = Nothing
    Set XL = Nothing


End Sub
 
Nice job Travis. Is there any way to have the excel workbook close without asking if I want to save the changes? My workbook has AutoOpen code running and I would not want to save the workbook when it is closed via the code that is opening it to read the worksheet names.

Thanks!
 
Thanks Travis thats exactly what I need. I spent ages looking through the knowledge base but couldn't find it.
 

Users who are viewing this thread

Back
Top Bottom