+ Reply to Thread
Results 1 to 7 of 7

Call sub from another open workbook

  1. #1
    Registered User
    Join Date
    10-01-2012
    Location
    Oslo, Norway
    MS-Off Ver
    Excel 2010
    Posts
    26

    Call sub from another open workbook

    Hi,
    I'm having difficulties running a sub that is located in another workbook.
    ex:
    My current program is located in the "master workbook" in "master module".
    The sub I want to call is located in "template" in "module1".

    Im thinking something like this:

    sub Program
    dim....

    for loop
    do something
    next
    call template!module1.sub
    do something again...
    end sub

    It might be useful that both workbooks are open when the program runs.
    Any help on this matter is appreciated.

    Regards
    JS.

  2. #2
    Valued Forum Contributor
    Join Date
    11-02-2012
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    564

    Re: Call sub from another open workbook

    You can do it in 2 ways.
    1. Add a reference to the workbook where your code to be called is existing (VBE Tools-->References)
    2. Use Run and specify workbook name and procedure name (Application.Run "'template.xlsm'!subname”). Make sure your Sub is in a module, not in excel objects

  3. #3
    Registered User
    Join Date
    10-01-2012
    Location
    Oslo, Norway
    MS-Off Ver
    Excel 2010
    Posts
    26

    Re: Call sub from another open workbook

    thx.
    if I use Run, i do not have refer to module?
    Do I have to write entire file path of template.xlsm?
    cant get it to work

  4. #4
    Registered User
    Join Date
    10-01-2012
    Location
    Oslo, Norway
    MS-Off Ver
    Excel 2010
    Posts
    26

    Re: Call sub from another open workbook

    by add a reference you mean
    set master = activeworkbook
    set template = workbooks("template.xlsm")
    ??
    then I would write
    call template.module1.sub ???

  5. #5
    Valued Forum Contributor
    Join Date
    11-02-2012
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    564

    Re: Call sub from another open workbook

    By Adding Reference, I mean you have to go to VBE-->Tools-->References and select or browse the workbook you need to have macros taken from. After adding this reference you can use macros in reference workbook as if macro is in the workbook you are working on.

  6. #6
    Valued Forum Contributor
    Join Date
    11-02-2012
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    564

    Re: Call sub from another open workbook

    Quote Originally Posted by Stroem View Post
    thx.
    if I use Run, i do not have refer to module?
    Do I have to write entire file path of template.xlsm?
    cant get it to work
    If you use Application.Run, no need to use entire file path, only file name would be enough. Make sure such file is open and macro is in a module (not under any sheet or ThisWorkbook code window)
    If still not getting through, please upload your both workbooks.

  7. #7
    Registered User
    Join Date
    10-01-2012
    Location
    Oslo, Norway
    MS-Off Ver
    Excel 2010
    Posts
    26

    Re: Call sub from another open workbook

    Haripopuri:
    Thanks again.
    Didnt get this method to work, but found it easier just to move all modules to the master file.
    If I find the time, I will try to make a sample file, where you can review my code. It would be nice to know where i failed.
    Unfortunatly the file im working on is confidential, so you cant review that.
    Regards
    JS.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1