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
Solved

Finding the Name on an Excel WorkSheet.

Posted on 2000-02-23
4
234 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
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…

840 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