如何使用Application.Run
命名参数?基于此MS文档:
您不能在此方法中使用命名参数。参数必须按位置传递。
就我的代码目的而言,强制按位置传递参数的解决方案似乎不太灵活,即创建一个通过传递一些参数来运行其他人的宏的宏。示例如下:
Sub MyMacro()
Dim macroName as String
Dim foo as String
Dim bar as String
macroName = "'quux.xlam'!quuz"
foo = "thud"
bar = "baz"
' arguments pass by position
' this method works
Application.Run macroName, foo, bar
' however, I intend to remove position dependency to allow flexibility
' hence, I'd like to pass argument by name, as below
Application.Run macroName, foo:=foo, bar:=bar ' or below
Application.Run macroName, bar:=bar, foo:=foo ' both raise compile error
End Sub
' Below is a macro from Add-In quux.xlam
Sub quuz(foo as String, bar as String)
MsgBox foo & bar
End Sub
我的问题与此类似,但那里没有回答我的问题。那么我如何找到替代品Application.Run
呢?
我有自己的宏。同时,我正在使用其他人的宏(例如,来自外接程序)。我们俩都没有交流,也没有适当的外接程序文档。潜在的情况如下(2):
插件代码是固定的
foo
,bar
但顺序未知。例如,我无法打开代码bcs宏受密码保护。我的代码是固定的
foo
,bar
但位置未知。我的实际情况更多地是数字2,所以我认为我的解决方案是按位置传递参数,并为其创建适当的文档。但是,我只是测试第一种情况,如果有人之前曾遇到过这种情况,或者有任何解决方案?
您可以编写一个包装程序,将变量按正确的顺序排序。
Sub test()
quuzWrapper bar:="bar", foo:="foo"
End Sub
Sub quuzWrapper(foo as String, bar as String)
Application.Run "quuz", foo, bar
End Sub
' Below is a macro from Add-In quux.xlam
Sub quuz(foo as String, bar as String)
MsgBox foo & bar
End Sub
确保您的VBA加载项具有唯一的VBA名称,并且未调用VBAProject
。例如称它quuxAddIn
。
在您的项目(不是加载项)中设置一个参考(在VB编辑器菜单中:Extras> References),然后选择quuxAddIn
。
然后,您可以像这样调用您的潜艇
quuxAddIn.quuz bar:="bar", foo:="foo"
根据注释进行编辑:
如果您设置了对项目的理解(工具提示),则应该起作用,并且在键入quuxAddIn.quuz之后,它应该显示带有参数的工具提示。这样,您还可以按名称提交参数。(但是实际上,该加载项必须存在才能添加为引用,因此仅适用于您的方案1)。对于方案2,仅提供您描述的解决方案以及按固定顺序使用Application.Run的解决方案。•然而,良好的文件记录应该是强制性的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句