PDA

View Full Version : How can I Run Macros In Other Excel File?



jiura
04-18-2008, 11:49 AM
I have two Excel files. I open 2-nd excel file with:
Workbooks.Open Filename:=Application.ActiveWorkbook.Path + "111.xls"

But then I want to run Macros which called "Macros 2". How can I do it from excel file 1?

figment
04-18-2008, 11:55 AM
that would depend on how macro 2 is writen and when do you wish to run it? if you want to run it as soon as the new workbook is created then it is easy, if you wish to work with the new work book for a while then run macro 2. then it becomes a bit harder.

tstav
04-18-2008, 12:00 PM
Try this
Application.Run "'OtherWbkName.xls'!MacroName(Arg1,Arg2)"

If the Macro has no arguments,skip the arguments in the above string.

figment
04-18-2008, 12:55 PM
tatav's solution works if macro 2 is stored in side the workbook you just opened.

tstav
04-18-2008, 01:21 PM
My post#3 code calls the MacroName macro which exists in the OtherWbkName.xls file.
This file can of course be already open, but it could be closed, too. It would work just the same.
Needless to say that the OtherWbkName.xls would open automatically for the Macro to run.

Edit: I should add that if the file is closed, we need to add the file's full path name e.g."'C:\MyFolder\1.xls'!MacroName"

figment
04-18-2008, 01:31 PM
tstav is there some other code that has to go along with the macro call code? for i cant for the life of me get it to work. does the macro your calling have to reside in a spacific module, or do you have to declare which module it's in?

tstav
04-18-2008, 01:37 PM
Hi figment,
I did a last minute edit to my last post, mentioning about adding the full path name in case the file is closed. Maybe you were reading my post as I was editing it...

Apart from that: no, I'm just entering the full path name of the file and the name of the macro. The macro is in the general code module. No special note for this module is needed.