Calling a sub from another workbook?

bcfaigg

Board Regular
Joined
Dec 1, 2005
Messages
78
Folks, I have a sub which amongst other things opens a workbook, and (hopefully) will run a macro in that workbook.

I know that generally, the code:
Application.Run "Workbook Name!Sub Name" achieves this.

My problem is that the Workbook Name may vary.
When I run that line of code, the relevant name of the workbook will be stored in a variable named "SesameLogName", and I wish to run a sub in that workbook (which is already open) called "Sesame".

Is there a way of generalising the above line of code in a format where the Workbook Name and Sub Names are in variables?

Application.Run Variable1!Variable2 doesn't work.

Any ideas?
Cheers
Gopes
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Gopes

Can't you just use string concatenation?

Something like this perhaps?
Code:
Application.Run SesameLogName & "!Sesame"
 
Upvote 0
Thanks Norie, sadly this doesn't seem to want to work.

I get a popup Runtime error '1004' message saying:

The macro 'Test Sesame Logger 20061012!Sesame' cannot be found.

The name of the workbook is in the case 'Test Sesame Logger 20061012', and the name of the macro is sub Sesame().

I have also tried concatenating ".xls!Sesame" as an alternative with the same error.

I.e.
Code:
Application.Run SesameLogName & ".xls!Sesame"

Any other ideas?

It is probably something really stupid and I will kick my self, but this one is really frustrating me.

Thanks
Gopes
 
Upvote 0
Got it - I knew I would kick myself...

The workbook name needs to be surrounded by apostrophe's e.g. 'Workbook Name'

Hence, to have the workbook name in a variable - e.g. WName, the code would look like:

Code:
WName="Test Sesame Logger 20061012"
Application.Run "'" & WName & ".xls'!Sesame"

Hope this is useful for other people too.
G
 
Upvote 0

Forum statistics

Threads
1,213,549
Messages
6,114,264
Members
448,558
Latest member
aivin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top