Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    jimmy.jowers08 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2015
    Posts
    8

    Question Opening Excel From Access

    I am a beginer level Access user. i have basic knowledge of how to build and manipulate an Access database. I am trying to write code to open multiple excel workbooks one at a time from Access. I succesfully wrote the code using a run command macro that opens one excel file. See Below:

    Function OpenExcelFromAccess()
    Dim MyXL As Object

    Set MyXL = CreateObject("Excel.Application")


    With MyXL
    .Application.Visible = True
    .Workbooks.Open "C:\Users\jameshe\Documents\.00estimate tools DB\MY GOODY BOX\Conduit Fill AND VOLTAGE DROPCalculator.xls"
    End With
    End Function

    My problemn is that i need to open several other workbooks but not all at the same time. When i try to add a macro/module to do the same thing as the one that works (with a different file path) I get an error code that says "The expression contains an ambiguous name." I tried adding this into the exisitng module and it will open both at the same time. Can someone help me understand what i am doing wrong.

    Set MyXL = CreateObject("Excel.Application")

    With MyXL
    .Application.Visible = True
    .Workbooks.Open "C:\Users\jameshe\Documents\.00estimate tools DB\MY GOODY BOX\Arc Flash Calculator.xls"
    End With


  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    I suggest you make a reference to the Microsoft Excel XX.X Object Library. You can do this from the VBA Editor. Go to Tools>References and then select the library by ticking the check box and clicking OK. With that you can use a different way to declare your Object Variables. I pasted some code below that uses this technique. The code opens a file in the path specified and renames Sheet1 to Bob. Check it out and see if that example helps at all.

    By using the reference to the library, you get the advantage of Intellisense as you type your code.

    Code:
        Dim xlx As New Excel.Application
        Dim xlw As Workbook
        Dim xls As Worksheet
        'Dim MyRange As Range
        Set xlw = xlx.Workbooks.Open("C:\Test\ExcelFiles\TestFile.xlsx")
        Set xls = xlw.Worksheets("Sheet1")
    '    Set xls = xlw.Worksheets(1)     'This option here is going after the index vs. the "name"
        
        xls.Name = "Bob"
        
        xlw.Save
        
        Set xls = Nothing
        xlw.Close
        Set xlw = Nothing
        Set xlx = Nothing

  3. #3
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    In addition to ItsMe's comments, you might check out Ken Snell's site:

    Ken's ACCESS Examples and Information for Working With EXCEL Workbook Files
    http://www.accessmvp.com/kdsnell/EXCEL_MainPage.htm

  4. #4
    jimmy.jowers08 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2015
    Posts
    8
    I must be missing something. I copied the code you supplied changing the file path and it keeps asking for a Macro name which it enters as a sub.

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Where did you put the function? Could be a Sub instead of Function.

    If all you want to do is open the Excel file and not manipulate it, consider FollowHyperlink or review http://allenbrowne.com/func-GoHyperlink.html or use Shell:

    Dim wsShell As Object
    Set wsShell = CreateObject("WScript.Shell")
    wsShell.Run Chr(34) & "C:\Test\ExcelFiles\TestFile.xlsx" & Chr(34)
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    jimmy.jowers08 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2015
    Posts
    8
    OK, what i am trying to open are a bunch of different calculation tools. For example, one workbook is a calculator to figure out conduit fill. I need to be able to manipulate certain fields in the excel to return values so i can't just open them as a read only. In the long run i will convert the excel workbooks into queries and forms in Access. In the interim i need to be able to open the files in Access. This seems like simple task but my lack of VBA knowledge is making it a chore.

    the code i am currently using was based on a google search so i dont understand what i did, i just know it works for one workbook only.

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Manipulate how? Do you need to set values in cells?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    jimmy.jowers08 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2015
    Posts
    8
    Basicallly i need to input values in the excel which performs the math inside of the workbook. i do not need to send the values back to Access. I just need to be able to open different workbooks from the same folder but only as i need them. Right now there are 4 workbooks that i need to include. Each one is a different tool. This is just a quick fix until i can actually re-create them in the Access program.

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    So you have 4 buttons with code in each Click event?

    Since you don't need to send data back to Access, probably want to set the Excel app to visible after programmatically setting values in cells.

    xlx.Visible = True

    instead of Close and Quit.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  10. #10
    jimmy.jowers08 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2015
    Posts
    8
    I will have 4 buttons on the Switchboard. Right now only one works. The code i originally posted works to open one. if i add the second set of code i posted to the first one i can open all 4 at the same time. i only want to open one in each instance?

  11. #11
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Quote Originally Posted by jimmy.jowers08 View Post
    I must be missing something. I copied the code you supplied changing the file path and it keeps asking for a Macro name which it enters as a sub.
    Can you post the code you used here? I suggest creating a new form and a new button. Start with a clean form and button.

  12. #12
    jimmy.jowers08 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2015
    Posts
    8
    Function OpenExcelFromAccess()
    Dim MyXL As Object


    Set MyXL = CreateObject("Excel.Application")
    With MyXL
    .Application.Visible = True
    .Workbooks.Open "C:\Users\jameshe\Documents\.00estimate tools DB\MY GOODY BOX\Conduit Fill AND VOLTAGE DROPCalculator.xls"
    End With
    End Function

  13. #13
    jimmy.jowers08 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2015
    Posts
    8
    Function OpenExcelFromAccess()
    Dim MyXL As Object


    Set MyXL = CreateObject("Excel.Application")
    With MyXL
    .Application.Visible = True
    .Workbooks.Open "C:\Users\jameshe\Documents\.00estimate tools DB\MY GOODY BOX\Conduit Fill AND VOLTAGE DROPCalculator.xls"
    End With
    Set MyXL = CreateObject("Excel.Application")
    With MyXL
    .Application.Visible = True
    .Workbooks.Open "C:\Users\jameshe\Documents\.00estimate tools DB\MY GOODY BOX\Arc Flash Calculator.xls"
    End With
    End Function


    This one opens both workbooks at the same time.

  14. #14
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Here is an example of two buttons.
    Attached Files Attached Files

  15. #15
    jimmy.jowers08 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2015
    Posts
    8
    im still tryig to figure out how to attach.
    Attached Thumbnails Attached Thumbnails Capture.PNG   Capture1.PNG  

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Excel Not Opening from Access
    By TinaH in forum Access
    Replies: 1
    Last Post: 01-08-2015, 03:33 PM
  2. Opening Excel in Access 2013
    By etorasso in forum Access
    Replies: 0
    Last Post: 12-18-2014, 03:10 PM
  3. Replies: 3
    Last Post: 12-15-2014, 06:43 PM
  4. Opening Excel Workbook From Access with VBA
    By kestefon in forum Access
    Replies: 4
    Last Post: 01-31-2014, 06:51 PM
  5. Prevent Excel opening Access Tables
    By Bishop426 in forum Security
    Replies: 4
    Last Post: 09-05-2011, 09:32 AM

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