?
Solved

How to load the names of all data sheets from an excel file?

Posted on 2003-03-10
3
Medium Priority
?
138 Views
Last Modified: 2010-05-18
Dear netters:

I am developing a VB program which opens an existing excel file and fill up the data into a user specified data sheet.  However, the excel file has multiple data sheets.  Is there any way to retrieve all the data sheet names from the excel file so the user can choose which data sheet to put the data in?

regards,
0
Comment
Question by:menshen_tsai
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 53

Accepted Solution

by:
Ryan Chong earned 225 total points
ID: 8103210
Try this example:

Private Sub Command1_Click()
    On Error GoTo EH
    Dim iExcel As Excel.Application
    Dim iWBook As Workbook
    Dim iWSheet As Worksheet
   
    tempdbpath = "c:\abc.xls"
   
    If tempdbpath = "" Then Exit Sub
    Set iExcel = CreateObject("excel.application")
    iExcel.Visible = True
    Set iWBook = iExcel.Application.Workbooks.Open(tempdbpath)
   
   
    For i = 1 To iWBook.Worksheets.Count
        Set iWSheet = iWBook.Worksheets(i)
        Debug.Print iWSheet.Name
    Next i
   
    'iWSheet.PrintOut
'    iWSheet.PrintPreview False
    'Set iWSheet = Nothing
    'iWBook.Close True
    'Set iWBook = Nothing
    'iExcel.Quit
    'Set iExcel = Nothing
    Exit Sub
EH:
    'ShowErrMsg
End Sub

cheers
0
 
LVL 12

Expert Comment

by:jgv
ID: 8103222
This will return all worksheets in the opened workbook:

Private Sub Command1_Click()
    Dim xApp As New Excel.Application
    Dim xSht As Excel.Worksheet
   
    xApp.Workbooks.Open "c:\My Documents\My Workbook.xls"
   
    For Each xSht In xApp.Worksheets
        Debug.Print xSht.Name
    Next
   
    Set xSht = Nothing
   
    xApp.Quit
    Set xApp = Nothing
End Sub
0
 

Expert Comment

by:CleanupPing
ID: 8900396
menshen_tsai:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Experts: Post your closing recommendations!  Who deserves points here?
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question