Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

excel

vba

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?

like image 489
Laurent Avatar asked Jan 26 '10 00:01

Laurent


People also ask

How do I remove spaces in Excel VBA?

To remove additional or extra spaces we use different VBA functions like LTRIM, TRIM, or RTRIM. We use LTRIM function to remove left most or starting of the string spaces. To remove right most or end of the string spaces we use RTRIM function.

How do I remove spaces between words in VBA?

Using trim (or trim$) in VBA will remove the leading and trailing spaces, which as mentioned is different from =TRIM in Excel. If you need to remove spaces (as mentioned below not necessarily all whitespace) from inside a string simply use WorksheetFunction. Trim . Save this answer.

How do you put a space in VBA code?

The VBA SPACE function is listed under the text category of VBA functions. When you use it in a VBA code, it returns a string which is consisting of a specified number of spaces. In simple words, with space function, you can create spaces by supplying numbers.

Can macro run on closed workbook?

You can cause Excel to run a macro automatically whenever a particular workbook is closed. For instance, when the workbook is closed you might want to run a macro that asks the users if they want to perform some task, such as saving the day's data to another file.


1 Answers

Use single quotes surrounding the filename:

Application.Run "'My Work Book.xls'!Macro1"
like image 52
e.James Avatar answered Oct 18 '22 21:10

e.James