'How to call function from another specific workbook in VBA?

I would like to know if there is a way to call a VBA function or method from another specified workbook's module as it is possible for a specific worksheet without using the Application.Run

For the worksheet I can call for example :

ActiveSheet.MyTest() 

if MyTest is defined in the sheet module

But I would like to call a function which is defined in a module

I tried :

ActiveWorkbook.MyTestModule()
ActiveWorkbook.VBProject.VBComponents("MyModule").MyTestModule(myArg)

which don't work generating an error Object does not support this method

I could call

Application.Run(ActiveWorkbook.name & "!MyTestModule", myArg)

But I am not sure of the error handling of the Application.Run and I would find cleaner to run directly the method



Solution 1:[1]

By the way, this also works if you want to access a custom data type in another workbook:

' In workbook ABC, project name Library

Public Type Book_Data
   Title As String
   Pub_Date As Date
   Pub_City As String
End Type

' In workbook DEF (after a ref to Library)
Dim Book_Info As Library.Book_Data

Book_Info.Title = "War and Peace"
Debug.Print Book_Info.Title

Solution 2:[2]

Note that if you want to call a method in a module of a different workbook without having the annoying behavior of a DLL-style link as mentioned in the other answers, you can use code like the below:

Dim otherWorkBook As Workbook
Set otherWorkBook = Workbooks.Open("myWorkbook.xlsm")

Call otherWorkBook.Sheets("SomeSheet").someMethod(arg1, arg2...)

Where someMethod() is a method which calls the actual method you're interested in within the module of the other sheet

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 RIck_R
Solution 2 Jack