11
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 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?

3 Answers 3

22

Use single quotes surrounding the filename:

Application.Run "'My Work Book.xls'!Macro1"
Sign up to request clarification or add additional context in comments.

3 Comments

In general, its would be better to always use quotes when dealing with filenames in Excel
I found the same issue with dashes ('-') in the file name too. Very annoying
I have found the same applies to the Macro name if it has underscores (_) in it. For some reason, this just came about with users on Windows 10.
6

Replace each of the spaces with %20

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

3 Comments

I know this is all from a very long time ago, but I think this is a perfectly valid answer.
Yes this is definitely an answer to the question and not a comment, @ClaricPWI is just wrong here
Yes this is an answer, not as useful as @e.James's since you might replace a lot of characters
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.