vba 从另一个工作簿运行 excel 宏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2806065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
running excel macro from another workbook
提问by l--''''''---------''''''''''''
I have a macro that is on a server. I need to be able to run it from different workstations that connect to this server.
我在服务器上有一个宏。我需要能够从连接到该服务器的不同工作站运行它。
Currently I am doing:
目前我正在做:
Application.Run ("L:\database\lcmsmacro\macro1.xlsm!macro_name")
The error message I am getting is "The macro may not be available in this workbook #1004"
我收到的错误消息是“此工作簿中的宏可能不可用 #1004”
I have already made sure that my security settings are set on the lowest level.
我已经确保我的安全设置设置在最低级别。
How do I run a macro from another workbook which is hosted on a different server?
如何从托管在不同服务器上的另一个工作簿运行宏?
would using add-ins help me?
使用加载项对我有帮助吗?
回答by Fink
I think your syntax is missing the single quote characters:
我认为您的语法缺少单引号字符:
Application.Run ("'L:\database\lcmsmacro\macro1.xlsm'!macro_name")
Then, if you needed to pass parameters to it the syntax would be like this:
然后,如果您需要将参数传递给它,语法将如下所示:
Application.Run ("'L:\database\lcmsmacro\macro1.xlsm'!macro_name","param1","param2")
回答by PaloDravecky
This error also shows up when there are duplicate macro names in the remote workbook, e.g. two macros named "macro_name". Took me a while to find out!
当远程工作簿中有重复的宏名称时也会出现此错误,例如两个名为“macro_name”的宏。我花了一段时间才知道!
回答by Alan Elston
Generally in Names a single ‘ is required if you have a space or punctuation in a name so that Excel does not get confused thinking that the space is a deliberate separation, such as in separating arguments from a Method. In some cases Excel will insist on them. Usually it does no harm to include them, even if they are not needed, for example when no spaces are present in names. Sometimes Excel will then take them out if they are not needed. http://www.eileenslounge.com/viewtopic.php?f=27&t=25599
通常在 Names 中,如果名称中有空格或标点符号,则需要一个 ' ,这样 Excel 就不会混淆,认为空格是有意的分隔,例如将参数与方法分开。在某些情况下,Excel 会坚持使用它们。通常包含它们并没有什么坏处,即使它们不是必需的,例如当名称中没有空格时。有时,如果不需要它们,Excel 会将它们取出。 http://www.eileenslounge.com/viewtopic.php?f=27&t=25599
回答by Sharunas Bielskis
If the macro you need to find relative macro path by using workbook path from which you run macro and you need to run several macros from the array list, the code below will help:
如果宏需要使用运行宏的工作簿路径来查找相对宏路径,并且需要从数组列表中运行多个宏,则以下代码将有所帮助:
Dim relativePath As String, programFileName As String
Dim selectedProgramsFiles() As String, programsArrayLastIndex As Byte, I As Byte
For I = 0 To programsArrayLastIndex 'Loop through all selected programs
programFileName = selectedProgramsFiles(I)
relativePath = ThisWorkbook.Path & "\" & programFileName
Workbooks.Open Filename:=relativePath
Application.Run ("'" & relativePath & "'!ModuleName.Main")
Workbooks(programFileName).Activate
ActiveWorkbook.Close SaveChanges:=False
Next I 'For I = 0 To programsArrayLastIndex 'Loop through all selected program

