user10144863
user10144863

Reputation:

Run Excel Macro from Access macro in VBA

I am trying to run an excel macro from access 2016. This has a lot of examples out their however they are all old. The exact error code I receive is run-time error '1004': cannot run the macro "TestScript()". The macro may not be available in this workbook or all macros may be displayed. I am only running something easy for the test. I have ensured that macros is enabled in excel. the excel code is as follows

Public Sub TestScript()
   MsgBox "IT WORKED"
End Sub

Real simple for the test. Access is opening the excel spreadsheet however it stops there with an error code.

My Access code is very simple and is below. I have also noted where the code stops. While I am new at VBA I have done a lot of research in this. I am testing as simple code as I could figure out. Any help would be welcomed.

Option Compare Database

Function runExcelmacro()

    Dim XL As Object
    
    Set XL = CreateObject("Excel.Application")
    
    With XL

        'Turn Off warnings
        .Visible = False
        .displayalerts = False
        'WorkBook path such as "C:\Computer\WorkBook.xlsx"
    
        .Workbooks.Open "C:\DATABASE\BLQ-10\Import Database BLQ 10\NTIRAINSTALLTO.xlsm"
        'Run the Macro in excel getworkbook

        .Run TestScript 'Code stops here!

        'Close Workbook
        .ActiveWorkbook.Close (True)
        .Quite
    End With
    Set XL = Nothing

End Function


Public Sub runMacroSub()
    Call runExcelmacro("C:\DATABASE\BLQ-10\Import Database BLQ 10\NTIRAINSTALLTO.xlsm", "TestScript")
End Sub

Upvotes: 0

Views: 7657

Answers (2)

Storax
Storax

Reputation: 12167

I guess OP did not put the code of Testscript in an extra module. Instead the code was put into the class module of the workbook or a worksheet. In the latter case you have to add the workbook or worksheet name in front of the name of the script.

Either it is `

.Run "ThisWorkbook.TestScript"

or in case it is in Sheet1 (codename of the sheet!)

.Run "Sheet1.TestScript"

Do not forget the quotation marks!

PS The OP's code above is working if you put testscript into a module and add quotation marks.

.Run "TestScript"

Here is a description how to create a module and add code

Upvotes: 1

Doug Coats
Doug Coats

Reputation: 7107

Here try this:

Public Sub TestScript()
   MsgBox "IT WORKED"
End Sub

Option Compare Database

Function runExcelmacro()
    Dim XL As Object, wb as object
    Set XL = CreateObject("Excel.Application")
    With XL
        .Visible = False
        .displayalerts = False
        set wb = .Workbooks.Open "C:\DATABASE\BLQ-10\Import Database BLQ 10\NTIRAINSTALLTO.xlsm"
        wb.Run TestScript 'Code stops here!
        .ActiveWorkbook.Close (True)
        .Quite
    End With
    Set XL = Nothing

End Function

Public Sub runMacroSub()
    Call runExcelmacro("C:\DATABASE\BLQ-10\Import Database BLQ 10\NTIRAINSTALLTO.xlsm", "TestScript")
End Sub

Here i declared wb as an object and then set wb = to the workbook.

This is of course assumin your test script is in the actual workbook being opened. Would be real funny if that wasnt a thing lol

Upvotes: 0

Related Questions