Solved

Finding the Name on an Excel WorkSheet.

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…

688 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