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

Application.Run "MyWorkBook.xls!Macro1"

will work (run the macro called Macro1 in the MyWorkBook.xls file)

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

will not (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?

Answers


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.

Need Your Help

Query a template for the variables it needs?

django variables django-templates

I'd like to be able to instantiate a template from a file (presumably using the django.template.loader.get_template(filename) ), and then determine the set of variables that should be defined in wh...

How to add Section Header in ListView List Item

java android listview

I want to Develop "History of Country" Android Application. But I have a problem. I can not put Country Header in Listview List Item. I want to put Header; Such as "Africa : North Africa", "Sub-Sah...