Curious Minds

Clear answers to science and history questions

Calling a Macro in a Different Module: How It Works

· 6 min read · Science

You can call a macro defined in a different module by using its fully qualified name—typically ModuleName.MacroName—provided the macro is declared as Public (or the equivalent in your language) and the calling code has a reference to that module. In most programming environments, macros are scoped to their module by default, so an unqualified call will fail unless you either qualify the name or explicitly import the macro into the current scope.

What "Macro" Means Across Languages

The term macro covers several different mechanisms, and the rules for calling one across modules depend heavily on which mechanism you are using. In C and C++, a macro is a preprocessor directive created with #define. In languages such as Rust, macros are syntactic extensions expanded at compile time. In VBA (Visual Basic for Applications), a macro is a subroutine stored in a module. In Lisp-family languages, macros are functions that run at compile time and transform code. Each has its own visibility and reference rules, but the core idea is the same: a macro lives inside a container (a module, a header, a file), and calling it from elsewhere requires making that container and the macro visible to the caller.

How Cross-Module Macro Calls Work

When you call a macro from another module, the compiler or interpreter must be able to resolve the name at the point of use. That resolution generally involves three things: the macro must be exported (public), the calling code must have access to the module's definition, and the name must be written in a form the toolchain recognises.

  • Export visibility: In VBA, a Sub or Function in a standard module is callable from other modules by default unless marked Private. In Rust, a macro must be annotated with #[macro_export] or re-exported with pub use to be used outside its crate. In C, a macro defined in a header file becomes visible to any source file that includes that header.
  • Reference or import: The calling module needs a path to the definition. That may be an #include directive, a use statement, a project reference, or a fully qualified name.
  • Qualified naming: Many languages let you write the module name followed by the macro name, such as Utilities.FormatDate in VBA or crate::macros::my_macro! in Rust. Qualification removes ambiguity when two modules define macros with the same name.

For example, in VBA you might have a module named Helpers containing Public Sub LogMessage(msg As String). From another module you can call Helpers.LogMessage "Started". If you omit Helpers., VBA will still find it as long as the name is unique across the project; qualification is mainly needed to avoid collisions or to make the dependency explicit.

Language-Specific Examples

C and C++ Preprocessor Macros

C macros are not scoped in the usual sense; they are textual substitutions performed by the preprocessor. A macro defined in mathutils.h is available to any .c file that contains #include "mathutils.h". There is no module system in classic C, so "calling a macro in a different module" really means including the header that defines it. C++ adds namespaces, and a macro can be placed inside a namespace, but the preprocessor ignores namespaces—the macro name is still global once the header is included. To avoid collisions, C and C++ projects often prefix macro names (e.g., MYLIB_MAX).

Rust Declarative Macros

Rust macros defined with macro_rules! are scoped to the module where they are defined unless exported. Adding #[macro_export] makes the macro available at the crate root, and other crates can call it as crate_name::macro_name!. Within the same crate, you can use use crate::macro_name; to bring it into scope, or refer to it by path. Procedural macros follow similar visibility rules but are defined in separate crates.

VBA Modules

In VBA, all standard modules in a project share a global namespace for public procedures. You can call Module2.MyMacro from Module1, or simply MyMacro if the name is unique. Class modules require an instance reference: myObject.MyMethod. The Private keyword restricts a procedure to its own module, which is the usual cause of "Sub or Function not defined" errors when calling across modules.

Common Problems and How to Fix Them

  • "Undefined macro" or "not found": The macro is not exported, the header or module is not imported, or the name is misspelled. Check visibility first, then the import path.
  • Name collision: Two modules define a macro with the same name. Use a qualified name or rename one macro. In C, prefixing is the standard fix.
  • Private or internal visibility: A macro marked private cannot be called from outside its module. Change it to public if appropriate, or add a public wrapper function that calls it internally.
  • Circular dependencies: Module A calls a macro in Module B, and B calls one in A. Most build systems handle this poorly. Refactor shared macros into a third module that both can reference.
  • Order of definition: In some languages (notably C preprocessor), a macro must be defined before the line that uses it. Including the header at the top of the file solves this.

Qualified vs. Unqualified Calls: When to Use Which

Unqualified calls are shorter and read more naturally when the macro name is unique and its origin is obvious. Qualified calls are safer in large codebases because they document where the macro comes from and prevent accidental shadowing. A common convention is to qualify calls across module boundaries and use unqualified calls only within the same module. In VBA, many developers qualify only when a name collision exists, because the editor's IntelliSense makes the source clear. In Rust, the path-based system encourages explicit qualification for exported macros.

Alternatives to Cross-Module Macro Calls

If calling a macro across modules proves awkward, consider these alternatives:

  • Wrapper functions: Define a public function in the macro's module that performs the same task. Functions have clearer scoping and type checking than macros in many languages.
  • Constants or inline functions: For simple value macros, a const or inline function often works better and avoids preprocessor pitfalls.
  • Re-export or facade modules: Create a single module that re-exports the macros you want to expose, giving callers one stable import path.
  • Code generation: In build systems, generate the needed definitions into the calling module so no cross-module reference is required.

Frequently Asked Questions

Can I call a macro from another module without qualifying its name?

Often yes, if the macro is public and its name is unique in the current scope. In VBA, public procedures in standard modules are globally visible. In C, the macro becomes visible once its header is included. In Rust, you typically need a use statement or a full path.

Why do I get a "Sub or Function not defined" error in VBA when the macro exists?

The most common reasons are that the macro is marked Private, it lives in a class module rather than a standard module, or the module name is misspelled in a qualified call. Check the procedure's declaration and the module type.

Are macros in different modules compiled separately?

It depends on the language. C preprocessor macros are expanded before compilation, so the definition must be textually included. Rust macros are expanded during compilation of the crate that uses them. VBA compiles the whole project together, so cross-module calls are resolved at compile time.

Can two modules define a macro with the same name?

Yes, but the caller must disambiguate. In C, the later definition may override or cause a redefinition warning. In Rust, paths distinguish them. In VBA, the project may report an ambiguous name error if both are public and unqualified.

Is calling a macro across modules slower?

No. Macro expansion happens at compile time or preprocessing time, so there is no runtime cost difference between a local and a cross-module macro call. The only cost is organisational: more dependencies and potentially longer build times.

In this article
  1. What "Macro" Means Across Languages
  2. How Cross-Module Macro Calls Work
  3. Language-Specific Examples
  4. Common Problems and How to Fix Them
  5. Qualified vs. Unqualified Calls: When to Use Which
  6. Alternatives to Cross-Module Macro Calls
  7. Frequently Asked Questions