Solved

OPen a Specific Excel sheet from a VB Button.

Posted on 2004-09-08
8
170 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
[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
  • 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
Technology Partners: 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!

 

Author Comment

by:spzbiegien
ID: 12011917
i GET RUN TIME ERROR "9" SUBSCRIPT OUT OF RANGE
0
 
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 34

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

733 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