Consulting

Results 1 to 7 of 7

Thread: How can I Run Macros In Other Excel File?

  1. #1

    How can I Run Macros In Other Excel File?

    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?

  2. #2
    VBAX Tutor
    Joined
    Aug 2007
    Posts
    273
    Location
    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.

  3. #3
    VBAX Mentor tstav's Avatar
    Joined
    Feb 2008
    Location
    Athens
    Posts
    350
    Location
    Try this
    [vba]Application.Run "'OtherWbkName.xls'!MacroName(Arg1,Arg2)"
    [/vba]
    If the Macro has no arguments,skip the arguments in the above string.
    He didn't know it was impossible, so he did it. (Jean Cocteau)

  4. #4
    VBAX Tutor
    Joined
    Aug 2007
    Posts
    273
    Location
    tatav's solution works if macro 2 is stored in side the workbook you just opened.

  5. #5
    VBAX Mentor tstav's Avatar
    Joined
    Feb 2008
    Location
    Athens
    Posts
    350
    Location
    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"
    He didn't know it was impossible, so he did it. (Jean Cocteau)

  6. #6
    VBAX Tutor
    Joined
    Aug 2007
    Posts
    273
    Location
    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?

  7. #7
    VBAX Mentor tstav's Avatar
    Joined
    Feb 2008
    Location
    Athens
    Posts
    350
    Location
    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.
    He didn't know it was impossible, so he did it. (Jean Cocteau)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •