当工作簿名称包含空格时在 Excel VBA 中使用 Application.Run

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2136768/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:14:13  来源:igfitidea点击:

Using Application.Run in Excel VBA when workbook name contains spaces

excelvbaexcel-vba

提问by Laurent

Application.Run "MyWorkBook.xls!Macro1"

will work (run the macro called Macro1in the MyWorkBook.xlsfile).

将工作(运行调用的宏Macro1MyWorkBook.xls的文件)。

Application.Run "My Work Book.xls!Macro1"

will not work (if the workbook name contains spaces, Excel says "the macro cannot be found").

将不起作用(如果工作簿名称包含空格,Excel 会提示“找不到宏”)。

I just spent hours figuring out that it's the spaces causing this problem...

我只是花了几个小时弄清楚这是导致这个问题的空间......

Is there a way around this without renaming the file?

有没有办法在不重命名文件的情况下解决这个问题?

回答by e.James

Use single quotes surrounding the filename:

在文件名周围使用单引号:

Application.Run "'My Work Book.xls'!Macro1"

回答by raymon

Replace each of the spaces with %20

将每个空格替换为 %20

Application.Run "My%20Work%20Book.xls!Macro1"

回答by Akshay Singh

Here File_nameis the name of the file for which you want to call your macro

这里File_name是您要为其调用宏的文件的名称

  • Application.run "'"+File_name+"'"+"!Macro"
  • Single quote in between double quote followed by + File_name + single quote in between double quote + your macro name in between double quotes.
  • Application.run "'"+File_name+"'"+"!Macro"
  • 双引号之间的单引号,然后是 + File_name + 双引号之间的单引号 + 双引号之间的宏名称。