Using Application.Run in Excel VBA when workbook name contains spaces
Application.Run "MyWorkBook.xls!Macro1"
will work (run the ma开发者_开发知识库cro called Macro1
in the MyWorkBook.xls
file).
Application.Run "My Work Book.xls!Macro1"
will not work (if the workbook name contains spaces, Excel says "the macro cannot be found").
I just spent hours figuring out that it's the spaces causing this problem...
Is there a way around this without renaming the file?
Use single quotes surrounding the filename:
Application.Run "'My Work Book.xls'!Macro1"
Replace each of the spaces with %20
Application.Run "My%20Work%20Book.xls!Macro1"
Here File_name is the name of the file for which you want to call your macro
- 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.
精彩评论