Archaimot
Archaimot

Reputation: 93

Call an Excel Macro with Variable file name

I know to run a macro in a module from vbscript it's as simple as

objexcel.Application.Run "Filename!MacroName"

But, what if the file name is variable--but the directory only contains one file (which has already been opened)?

Could it be as simple as:

objexcel.Application.Run wkbk.path "Macroname"

Upvotes: 0

Views: 1783

Answers (2)

Archaimot
Archaimot

Reputation: 93

It works with

objexcel.run "MacroName"

Upvotes: 1

Rory
Rory

Reputation: 34075

Assuming wkbk is a workbook object, you can use:

objexcel.Application.Run "'" & wkbk.Name & "'!Macroname"

the single quotes are there in case the workbook name contains spaces. Note you don't need the path since the workbook is open.

Upvotes: 0

Related Questions