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?

Yoboi
  • 19
  • 9
Laurent
  • 5,953
  • 14
  • 43
  • 59

3 Answers3

22

Use single quotes surrounding the filename:

Application.Run "'My Work Book.xls'!Macro1"
e.James
  • 116,942
  • 41
  • 177
  • 214
  • In general, its would be better to always use quotes when dealing with filenames in Excel – Ahmad Nov 16 '10 at 11:46
  • I found the same issue with dashes ('-') in the file name too. Very annoying – David Hayes Aug 16 '11 at 15:05
  • 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. – CarloC May 08 '18 at 20:52
6

Replace each of the spaces with %20

Application.Run "My%20Work%20Book.xls!Macro1"
Conner
  • 30,144
  • 8
  • 52
  • 73
raymon
  • 61
  • 1
  • 1
  • 9
    I know this is all from a very long time ago, but I think this is a perfectly valid answer. – e.James Apr 13 '13 at 17:32
  • Yes this is definitely an answer to the question and not a comment, @ClaricPWI is just wrong here – cowls Mar 27 '14 at 13:51
  • Yes this is an answer, not as useful as @e.James's since you might replace a lot of characters – GôTô Jul 25 '14 at 12:40
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.
Akshay Singh
  • 153
  • 1
  • 7