• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1106
  • Last Modified:

Excel VBA - How to call module procedure from Workbook Object after instanciated

Is there a way to call and function module from the workbook object after instanciated based on the below scenerio...

The Book1.xlsm contain the macro that open up the Book2.xlsm and is set to workbook object.


sub openfile()
      Dim wb as Workbooks.
      Set wb= Workbooks.Open("C:\test\Book2.xlsm",  ReadOnly:=True)
       wb.module1.test    '<-- how to call the procedure in his own module
       wb.module1.test1(wb) <-- how to call the procedure in his own module
End sub


in Book2.xlsm

sub test1()
        msgbox ("Hi")

end sub

sub test2( wb as workbooks)
        bla bla bla...
end sub

Thank you.

best regards
3 Solutions
You could use this kind of code:-
Application.Run "'My Workbook.xlsm'!ThisModule.ThisProcedure"

Open in new window

The procedure you call must be declared as Public (default).
First Part Book1.xlsm
If the procedure is test like
Sub Test
End Sub

then you can call it simply by naming it
Call test

if test has a parameter wb then also you call it
test wb
or Call test wb

Second part book2.xlsm
you have test1 and test2 subs not clear on how you want to call them from where
if you are in book2 like it is the active workbook then same as before simply name them
if you are calling them from book1 then
they have to public sub like

Public sub test1()
        msgbox ("Hi")

end sub

Public sub test2( wb as workbooks)
        bla bla bla...
end sub

now this is a bit delicate and need following code

Dim Wbmacro As String
Set WB = Workbooks.Open("Book2.xlsm")

Wbmacro = "'" & WB.FullName & "'!" & "Module1.test1"
Application.Run (Wbmacro)

Open in new window

Note the single quote that surround the fullpathname of the book2
Note also the modulename containing test1

Now if you want to call test2 passing it WB which is WB1 then you do this

Dim WB1 as workbook
Dim WB2 as workbook
Dim Wbmacro As String

Set WB1 = Activeworkbook
Set WB2 = Workbooks.Open("Book2.xlsm")

Wbmacro = "'" & WB2.FullName & "'!" & "Module1.test2"
Application.Run (Wbmacro,WB1)

Open in new window

Note we are passing WB1 as an argument in the application Run

Try it and let me know. One last thing when you open Book2.xlsm I assume that it is in the same directory as book1 reason why I only put its name if it is not you need to supply the fullpath to book2 or else you will get File not found.
Public properties subs and functions that are defined in the ThisWorkbook module show up as properties and methods of the object, so if you have in ThisWorkbook of C:\test\Book2.xlsm code like this

Public Property Get WbkVersion() As String
    WbkVersion = "2.32"
End Property

Public Sub HelloWorld()
    MsgBox "Hi"
End Sub

Open in new window

You can then in your code say something like:
Sub openfile()

    Dim wb As Workbook
    Set wb = Workbooks.Open("C:\test\Book2.xlsm", ReadOnly:=True)
    MsgBox wb.WbkVersion
    Call wb.HelloWorld
End Sub

Open in new window

This is late bound, so you won't get intellisense for those properties and methods, but they are available at run time. You could of course get these routines to call into other code modules in your Book2.xlsm.
meniykAuthor Commented:
Thank you Faustulus, gowflow and andrewssd3.
Greatly appreciate you response and contribution.
That's amazing, Andrewssd3 manage to reslove my the problem and very well explain the concept. Once again Thank you Andrewssd3. Point has been mostly rewarded to Andrewssd3.
Great input. We never cease to learn it opens up new horizons. Tks again for your valuable input.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now