Results 1 to 6 of 6
  1. #1
    SltPhx is offline Advanced Beginner
    Windows Vista Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    41

    Question Open specific worksheet from Access VBA

    Hello All,
    I am trying to open a pecific worksheet in an excel workbook from an access form.
    The form has a contro 'ControlCharts' - And 'on Click' I want to add a VBA to open this specific worksheet from a folder on my desktop.
    I am able to open the workbook - but not the specific worksheet.
    This is the code I used -

    Private Sub ControlCharts_Click()
    Set xlApp = CreateObject("Excel.Application")


    xlApp.Visible = True
    xlApp.Workbooks.Open "Path", True, False
    Set xlApp = Nothing
    End Sub

    Kindly let me know the answer to this. Thanks in advance

  2. #2
    manic is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2012
    Posts
    63
    You can try something like this:
    Code:
    Private Sub CmdOpenExcelSheet_Click()
    On Error GoTo Err_CmdOpenExcelSheet_Click
    
    
      Dim xlApp As Excel.Application
      Dim xlWkbk As Excel.Workbook
      Dim xlSht As Excel.Worksheet
    
    
      Set xlApp = New Excel.Application
    
    
      xlApp.Visible = True
      
    'Set the workbook and the filepath 'Change "C:\YourExcelFile.xls " to your own filepath and Workbook name
      Set xlWkbk = xlApp.Workbooks.Open("C:\Book1.xls ")
    
    'Set the worksheet that you want the workbook to open on
      xlApp.Sheets("Sheet3").Select
      
     'Turn everything off otherwise you will have problems with your spreadsheet
      Set xlApp = Nothing
      Set xlWkbk = Nothing
      Set xlSht = Nothing
      
    Exit_CmdOpenExcelSheet_Click:
        Exit Sub
    
    Err_CmdOpenExcelSheet_Click:
        MsgBox Err.Description
        Resume Exit_CmdOpenExcelSheet_Click
        
    End Sub

  3. #3
    SltPhx is offline Advanced Beginner
    Windows Vista Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    41
    Thank you so much That worked.

    I had to remove

    Dim xlApp As Excel.Application
    Dim xlWkbk As Excel.Workbook
    Dim xlSht As Excel.Worksheet

    And use
    Set xlApp = CreateObject("Excel.Application")

    Do you know the reason? I am not very familiar with VBA coding. Please explain if you do know, why.
    Thank you so much for your time and effort. I really appreciate it.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You're using late binding, perhaps unintentionally; manic is using early binding, which requires a reference be set:

    http://support.microsoft.com/default...;en-us;Q245115
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    SltPhx is offline Advanced Beginner
    Windows Vista Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    41
    Thank you so much Paul.
    That link was helpful.
    How will I mark this as solved?

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    No problem. You can mark the thread solved using "Thread Tools" near the top of the page.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 08-22-2012, 06:28 AM
  2. Replies: 4
    Last Post: 11-09-2011, 08:40 AM
  3. Replies: 6
    Last Post: 11-05-2011, 09:01 AM
  4. Open a specific Excel application in Access
    By Hobbes29 in forum Programming
    Replies: 1
    Last Post: 02-14-2011, 06:48 PM
  5. Replies: 1
    Last Post: 11-21-2010, 10:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums