?
Solved

Finding the Name on an Excel WorkSheet.

Posted on 2000-02-23
4
Medium Priority
?
245 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 400 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

Introducing Priority Question

Increase expert visibility of your issues by participating in Priority Question, our latest feature for Premium and Team Account holders. Adjust the priority of your question to get emergent issues in front of subject-matter experts for help when you need it most.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
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 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…
Suggested Courses
Course of the Month14 days, 18 hours left to enroll

771 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