Solved

OPen a Specific Excel sheet from a VB Button.

Posted on 2004-09-08
8
166 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
Comment Utility
This works for me.

ExcelWbk.Sheets("SheetName").Activate
0
 

Author Comment

by:spzbiegien
Comment Utility
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
Comment Utility
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
Comment Utility
i GET RUN TIME ERROR "9" SUBSCRIPT OUT OF RANGE
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 76

Expert Comment

by:David Lee
Comment Utility
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
Comment Utility
Thank you,  I Spelled Forecast wrong!
0
 
LVL 76

Expert Comment

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

Accepted Solution

by:
Mike Eghtebas earned 500 total points
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

772 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

12 Experts available now in Live!

Get 1:1 Help Now