简体   繁体   English

在另一个工作簿中的工作簿中运行vba宏

[英]run a vba macro in a workbook from another workbook

I am trying to write a macroB in WorkbookB which opens another WorkbookA and runs macroA in sheet1. 我试图在WorkbookB中编写一个macroB ,它会打开另一个WorkbookA并在sheet1中运行macroA

I googled and found two methods: 我用谷歌搜索,发现了两种方法:

  • Using Application.Run 使用Application.Run

     Dim i As Integer, en As Integer en = Range("B4") Application.Run "C\\Mtest\\WorkbookA.xlsb !macroA" 

    But I get the error 1004: either the macro dose not exist in this sheet or the macro is deactivate 但我收到错误1004:此工作表中不存在宏指令或宏已停用

  • Using RunAutoMacros 使用RunAutoMacros

     Workbooks.Open "C\\Mtest\\WorkbookA.xlsb" Workbooks("WorkbookA.xlsb").Worksheets("sheet1").Activate ActiveWorkbook.RunAutoMacros macroA 

    And here I get the error: 1004 the method RunAutoMacros does not work. 在这里,我得到了一个错误:1004 RunAutoMacros方法不起作用。

Any idea what is wrong? 知道有什么问题吗? Or how can I do it in a better way? 还是我能以更好的方式做到这一点?

假设您的macroA在工作簿C\\Mtest\\WorkbookA.xlsbsheet1的代码模块中,则应使用

Application.Run "'C\Mtest\WorkbookA.xlsb'!sheet1.macroA"

I think the space is the problem. 我认为空间是问题。 Try removing it. 尝试将其删除。 ie - change this line 即-更改此行

Application.Run "C\Mtest\WorkbookA.xlsb !macroA"

to

Application.Run "C\Mtest\WorkbookA.xlsb!macroA"

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

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