Results 1 to 4 of 4
  1. #1
    Sck is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Jul 2010
    Posts
    82

    Call Excel subroutine with a parameter from Access

    I am trying to run an Excel VBA procedure from Access and can't figure out how to send the required parameter. This works for running one that does not require a parameter:

    xlApp.Run sFileName & "!" & sMacroName

    this is the subroutine Sub FormatDollars(MonthEnd As String)

    this is what I tried:
    xlApp.Run sFileName & "!" & sMacroName & ", '" & CStr(MEnd) & "'"
    and
    xlApp.Run sFileName & "!" & sMacroName & ", " CStr(MEnd)



    Access breaks on this line. Error 1004, Cannot run the macro 'FormatFile.xlsm!FormatDollars, 201412' this macro may not be available.....
    if I am right, it is reading the parameter as part of the macro name and not a parameter.

    any help is appreciated.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Should work https://msdn.microsoft.com/en-us/lib...ffice.11).aspx

    This works for me:

    Sub test()
    Dim oExcel As Excel.Application
    Dim oBook As Excel.Workbook
    Set oExcel = CreateObject("Excel.Application")
    Set oBook = oExcel.Workbooks.Open("C:\Temp\Book1.xlsm")
    oExcel.Run "Test", "This is a test"
    oExcel.Visible = True
    Set oBook = Nothing
    Set oExcel = Nothing
    End Sub
    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.

  3. #3
    Sck is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Jul 2010
    Posts
    82
    Thanks, I got it to work by hardcoding the macro name. It is not the best option but it works for this application.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    My code did not include the file name in the Run method.

    I just tested using a variable for the procedure name argument. That also works.
    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.

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

Similar Threads

  1. Replies: 12
    Last Post: 09-15-2014, 04:52 PM
  2. Passing Parameter from Excel TO Access
    By jo15765 in forum Queries
    Replies: 1
    Last Post: 11-29-2011, 03:28 PM
  3. Calling Access VBA subroutine from an Excel VBA subroutine
    By richard_yolland in forum Programming
    Replies: 0
    Last Post: 02-16-2011, 11:30 AM
  4. Replies: 0
    Last Post: 05-07-2009, 02:32 PM
  5. Call Excel Data into Access table
    By jiguvaidya in forum Import/Export Data
    Replies: 0
    Last Post: 09-15-2008, 04:58 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