简体   繁体   English

从 VBA 中的 Access 宏运行 Excel 宏

[英]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.我正在尝试从 access 2016 运行一个 excel 宏。这有很多例子,但它们都很旧。 The exact error code I receive is run-time error '1004': cannot run the macro "TestScript()".我收到的确切错误代码是运行时错误“1004”:无法运行宏“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.我确保在 excel 中启用了宏。 the excel code is as follows excel代码如下

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. Access 正在打开 Excel 电子表格,但它在那里停止并显示错误代码。

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.虽然我是 VBA 的新手,但我在这方面做了很多研究。 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

I guess OP did not put the code of Testscript in an extra module.我猜 OP 没有将Testscript的代码放在一个额外的模块中。 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!)或者如果它在Sheet1 (工作表的代号!)

.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. PS如果您将testscript放入模块并添加引号,则上面的 OP 代码正在运行。

.Run "TestScript"

Here is a description how to create a module and add code 以下是如何创建模块和添加代码的说明

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.在这里,我将 wb 声明为一个对象,然后将 wb = 设置为工作簿。

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如果那不是一件事,那就太有趣了,哈哈

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM