Solved

Finding the Name on an Excel WorkSheet.

Posted on 2000-02-23
4
232 Views
Last Modified: 2010-08-05
I am working on a program that is importing data from Excel.  I have the user select a worksheet, and the the import begins, the problem I have is that the worksheet name are a bit different.  When I access the worksheet via:
   Set ExcelApp = New Excel.Application
   ExcelApp.Visible = True
     
   ExcelApp.Workbooks.Open txtNASBeginPath
   Set ExcelBook = ExcelApp.ActiveWorkbook
   
   Set ExcelSheet = ExcelBook.Worksheets(1)

This gives me the last active worksheet, but I get back bad data.

Now if the sheet was named "QTR1 99" then I can set
   Set ExcelSheet = ExcelBook.Worksheets("QTR1 99") and the data comes out fine.  This is my predicament, The name is alway different, can I programatically obtain a collection of sheets from the Excel Object?

I know this seems confusing, but any help is greatly appreciated.

Thank you in Advance.

-John
0
Comment
Question by:jforbes
4 Comments
 
LVL 20

Expert Comment

by:hes
ID: 2552048
Here is how I do it:

Private Sub Form_Load()
Dim vbexcel As Excel.Application
Dim wkbObj As Workbook
Set vbexcel = CreateObject("Excel.Application")
Set wkbObj = GetObject("YourExcelName.xls")

'Set vbexcel.ActiveWorkbook("F:\vb\testord\Set Order.xls")
'Set mysheets = wkbObj.Worksheets
For Each wks In wkbObj.Worksheets
 Debug.Print wks.Name
Next
End Sub
0
 
LVL 2

Accepted Solution

by:
danlevans earned 100 total points
ID: 2552068
jforbes,

For Each ws In Worksheets
      MsgBox ws.Name
Next ws


Have fun

Dan
0
 
LVL 3

Expert Comment

by:Lewy
ID: 2552070
Try this:

    Dim PN As New Collection, PX
    Dim I As Integer, Row As Long, P As Integer
   
    For I = 1 To Worksheets.Count
        PN.Add (Worksheets(I).Name)
    Next I
   
    For Each PX In PN
        Worksheets(PX).Activate
    Next PX
    'or
    Worksheets("Sheet2").Activate
0
 
LVL 1

Author Comment

by:jforbes
ID: 2552253
Thanks All, I should have guessed it.
-John
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

831 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