Solved

OPen a Specific Excel sheet from a VB Button.

Posted on 2004-09-08
8
168 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
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

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 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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…

773 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