+ Reply to Thread
Results 1 to 7 of 7

VBA code to execute a module in a different workbook

Hybrid View

  1. #1
    Registered User
    Join Date
    12-27-2011
    Location
    -
    MS-Off Ver
    Excel 2007
    Posts
    11

    VBA code to execute a module in a different workbook

    Like the title says: I want to execute a module which is located in a certain workbook by clicking a button in a different workbook.
    This all is also in the picture attached.

    Please have a look.
    Thanks


    16lk50k.jpg

  2. #2
    Valued Forum Contributor
    Join Date
    08-29-2011
    Location
    Mississauga, CANADA
    MS-Off Ver
    Excel 2010
    Posts
    503

    Re: VBA code to execute a module in a different workbook

    Use this:
    change the red words so they match what you have.

    Sub RunRemote()
    Dim oWB As Workbook
    Set oWB = Workbooks.Open("D:\PLCHKSPTL.xls")
    Application.Run (oWB.Name & "!UpdBtn_Update")
    oWB.Close
    End Sub
    Regards,
    Khaled Elshaer
    www.BIMcentre.com

    Remember To Do the Following....
    1. Thank those who have helped you by clicking the Star below their post.
    2. Mark your post SOLVED if it has been answered satisfactorily:
    • Select Thread Tools (on top of your 1st post)
    • Select Mark this thread as Solved

  3. #3
    Forum Contributor
    Join Date
    04-11-2011
    Location
    Columbus, Ohio
    MS-Off Ver
    Excel 2007
    Posts
    325

    Re: VBA code to execute a module in a different workbook

    http://www.mrexcel.com/archive/VBA/15551.html

    The above link shows how to work with different workbooks. You just have to apply it correctly.

  4. #4
    Valued Forum Contributor
    Join Date
    08-29-2011
    Location
    Mississauga, CANADA
    MS-Off Ver
    Excel 2010
    Posts
    503

    Re: VBA code to execute a module in a different workbook

    or you can use this without any variables.

    Sub RunRemote()
    Application.Run (Workbooks.Open("D:\PLCHKSPTL.xls").Name & "!UpdBtn_Update")
    Workbooks("PLCHKSPTL.xls").Close
    End Sub

  5. #5
    Registered User
    Join Date
    12-27-2011
    Location
    -
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: VBA code to execute a module in a different workbook

    Thanks for the responses.
    It seems to work fine except if the file has spacings in its name.
    I don't know if that's the reason it don't work in those cases or if there's another reason.
    I just noticed it only happened so far when the file has spacings in its name.

    The error I get says something about macros disabled. I checked but all macros are enabled.

    See the attachment

    untitled.jpg

  6. #6
    Registered User
    Join Date
    12-27-2011
    Location
    -
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: VBA code to execute a module in a different workbook

    it's definitely the space in the name
    (when i removed it the code worked fine).

    So my question is what to change in the code to make it working for files with spaces in their name also.

    Thanks in advance

  7. #7
    Registered User
    Join Date
    12-27-2011
    Location
    -
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: VBA code to execute a module in a different workbook

    Made some changes to the code so I could put the file name between an extra pair of single quotes ' ' to deal with spaces and "-" in file names:

    Private Sub CommandButton1_Click()
        Workbooks.Open ("\\Nw_edc_02\apps1\Starquery\Werkfolder\WR\xls\aantal picks.xls")
        Application.Run ("'aantal picks.xls'!Updbtn_Update")
    End Sub

    Found here: http://stackoverflow.com/questions/2...ontains-spaces

+ 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