Solved

OPen a Specific Excel sheet from a VB Button.

Posted on 2004-09-08
8
167 Views
Last Modified: 2010-05-02
OK, I can open an Excel Spreadsheet from a button, but how do I open a specific sheet inside the Excel Workbook.

Here is the code to open an excel Workbook:

Set ExcelWBk = Excel.Workbooks.Open("H:\Comets Portland\Comets\Excel\Queue Age\que aging" )

How would I make it open a worksheet name "HB PRIME"?
0
Comment
Question by:spzbiegien
  • 4
  • 3
8 Comments
 
LVL 76

Expert Comment

by:David Lee
ID: 12011548
This works for me.

ExcelWbk.Sheets("SheetName").Activate
0
 

Author Comment

by:spzbiegien
ID: 12011788
It doesn't work for me!  Where would you add the code to make it work.  See Code Below!


Private Sub CmdQuarter3_Click()
Dim Excel As Excel.Application
Dim ExcelWBk As Excel.Workbook
Dim ExcelWS As Excel.Worksheet

           
            Form1.Animation1.Open "H:\Comets Portland\Comets\comtest2.avi"
            Form1.Animation1.Visible = True
            Set Excel = CreateObject("Excel.Application")
            With Excel
                .Visible = True
            End With
           
            Set ExcelWBk = Excel.Workbooks.Open("H:\Comets Portland\COMETS\Excel\CometsForecasts\Q3 2004")
            Set ExcelWS = Excel.Sheets("CIB Forcast").Activate
            Form1.Animation1.Visible = False
            Exit Sub
 
End Sub
0
 
LVL 76

Expert Comment

by:David Lee
ID: 12011854
Two problems.  First, use the workbook object, not the Excel object.  Second, don't use Set on the command to activate a sheet.  In the code above replace

    Set ExcelWS = Excel.Sheets("CIB Forcast").Activate

with

    ExcelWBk.Sheets("CIB Forcast").Activate
0
 

Author Comment

by:spzbiegien
ID: 12011917
i GET RUN TIME ERROR "9" SUBSCRIPT OUT OF RANGE
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 76

Expert Comment

by:David Lee
ID: 12011952
That sounds to me as if the sheet name isn't correct.  I created a workbook here and added a sheet with the name "CIB Forcast" and tested the code.  It worked fine.
0
 

Author Comment

by:spzbiegien
ID: 12011963
Thank you,  I Spelled Forecast wrong!
0
 
LVL 76

Expert Comment

by:David Lee
ID: 12011986
No problem, I do things like that all the time.
0
 
LVL 33

Accepted Solution

by:
Mike Eghtebas earned 500 total points
ID: 12012866
Private Sub CmdQuarter3_Click()

         Dim XlObj As Excel.Application                '<-- note XlObj in place of Excel
         Dim ExcelWBk As Excel.Workbook
         Dim ExcelWS As Excel.Worksheet

           
            Form1.Animation1.Open "H:\Comets Portland\Comets\comtest2.avi"
            Form1.Animation1.Visible = True
            Set XlObj = GetObject(,"Excel.Application")      
            XlObj .Visible = True
           
            Set ExcelWBk = XlObj .Workbooks.Open("H:\Comets Portland\COMETS\Excel\CometsForecasts\Q3 2004")
            Set ExcelWS = XlObj.Worksheets("CIB Forcast")
            ExcelWS.Activate
            Form1.Animation1.Visible = False
 
End Sub
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we 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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now