Introduction
In VBA, as the scale of a project grows, code is often managed by splitting it into different standard modules based on functionality. In such cases, there is a possibility of accidentally defining macros (procedures) with the same name in different modules.
If you call a macro by its name alone in this situation, VBA may not know which macro to execute, leading to the execution of the unintended macro or causing errors.
This article explains how to safely execute procedures in other modules by explicitly stating the module name, thereby accurately telling VBA which module’s macro to call.
Sample Code: Calling a Macro with a Module Name
In this example, we will create two standard modules:
- MainModule: The module that performs the main processing.
- StringHelpers: A library module that groups generic functions related to string manipulation.
1. Create the Library Module
First, create a module to store generic functions. Change the module name to StringHelpers in the Properties window.
[Standard Module: StringHelpers]
' Function to add brackets around a string
Public Function AddBrackets(ByVal text As String) As String
AddBrackets = "[" & text & "]"
End Function
' Procedure to output a simple log
Public Sub PrintLog(ByVal message As String)
Debug.Print Date & " " & Time & ": " & message
End Sub
2. Call from the Main Module
Next, call the procedures of the StringHelpers module created above from MainModule, specifying the module name.
[Standard Module: MainModule]
' Main process calling procedures from another module
Sub RunMainProcess()
Dim formattedText As String
'--- Call a Sub procedure by specifying the module name ---
Call StringHelpers.PrintLog("Starting process")
'--- Call a Function procedure by specifying the module name ---
formattedText = StringHelpers.AddBrackets("Important Notice")
MsgBox formattedText
Call StringHelpers.PrintLog("Ending process")
End Sub
Execution Results
When RunMainProcess is executed, a message box displaying “[Important Notice]” appears, and a log similar to the following is output to the Immediate Window:
Plaintext
2025/08/17 15:11:46: Starting process
2025/08/17 15:11:46: Ending process
Explanation of the Code
Call StringHelpers.PrintLog(...)
This is the syntax for explicitly calling a procedure from another module.
- StringHelpers.: Write the name of the module to which the procedure belongs before the procedure name, connected by a dot (
.). - Call: A keyword for calling a Sub procedure (optional, but adding it makes the call clear).
formattedText = StringHelpers.AddBrackets(...)
When calling a Function procedure, write it in the format ModuleName.FunctionName in the same way.
Why Specify the Module Name?
- Improved Code Readability: Just by writing
StringHelpers.PrintLog, the intent of the code becomes immediately clear: “This is a log output feature located in the string helper module.” - Avoiding Name Conflicts: Even if
MainModulealso had a procedure namedPrintLog, writingStringHelpers.PrintLogensures that theStringHelpersmodule’s version is called. This prevents unintended behavior and improves macro stability.
Summary
In this article, I explained how to call procedures located in other standard modules by specifying the module name.
- You can explicitly specify the destination using the syntax
ModuleName.ProcedureName. - This improves code readability and stability.
Especially in large-scale projects consisting of multiple modules or during team development, it is recommended to thoroughly adopt this coding style to clearly state the caller.
