Calling private procedures from one VBA module to another in Excel VBA is a powerful way to modularize code, but it requires careful setup. Yes, it works—you simply reference the module containing the private sub, pass any required parameters, and execute it. The key challenge isn’t the concept, but ensuring your modules are correctly linked and the private subs are accessible through the correct scope. Missing this can lead to errors like "Sub not found" or "Access violation" crashes.
What You'll Need
- A standard Excel workbook with at least two modules (e.g.,
Module1andModule2) - Private procedures defined in one module (e.g.,
Module1) - An application or user event that needs to call these procedures (e.g., a button click, worksheet change, or timer)
- The VBA editor opened in Excel (press
Alt+F11to access it)
Step-by-Step
-
**Define the Private Procedure in the Target Module (e.g.,
Module1)**. In the VBA editor, locate the module where you want to hide the procedure. For example, inModule1, add a private sub like this:Private Sub ProcessData(inputRange As Range) Debug.Print "Processing data from: " & inputRange.Address ' Add your logic hereProcessDatais now private—only accessible withinModule1. -
**Reference the Module Where the Procedure Resides**.
From the procedure calling the private sub (e.g.,
Module2or a user event), use the fully qualified name to call it. For example:' In Module2 (or a user event) Set myDataRange = ThisWorkbook.Sheets("Sheet1").Range("A1:A10") Module1.ProcessData myDataRangeKey points:- Use
ModuleName.ProcedureNameto bypass "private" restrictions. - If the module is in a different project (like a separate VBA file), prefix with
ThisWorkbook.Namespace.Nameor the project’s module path.
- The module is actually defined in the workbook.
- You’re not using the wrong project scope (e.g.,
ThisWorkbook.Sheetsinstead ofModule1).
- Use
-
**Pass Required Parameters Correctly**.
Private procedures can require arguments. Use explicit typing (e.g.,
Range,String) and pass values as needed. Example:' Caller in Module2 Module1.ProcessData Range("B1"), "Custom Value"For complex data (e.g., arrays), use references or return values:' Alternative: Return multiple values Dim result As Variant result = Module1.GetMultipleValues(Range("C1:C5")) -
**Trigger the Call from a User Event**.
Attach the procedure call to an event (e.g., button click, worksheet change) in the VBA editor:
- Double-click a button in the Excel workbook to open the code editor.
- Press
F8to insert a call to Module1.ProcessData (e.g., afterWorksheet_ChangeorButton_Click).
Private Sub Button1_Click() Module1.ProcessData ThisWorkbook.Sheets("Sheet1").Range("D1:D10")
Common Mistakes
-
**Assuming "private" restricts all calls**—it only restricts access within the *owning module*. To call it from another module, use the fully qualified name (e.g.,
Module1.ProcessData). - **Forgetting to include the module name**—Excel may resolve it dynamically, but hardcoding just the procedure name fails unless the module is visible in the project explorer.
-
**Using
Public Subinstead ofPrivate Sub**—this exposes the procedure globally, defeating the purpose of modularity. -
**Ignoring scope errors**—if the module is in a separate project file, reference it via
ThisWorkbook.Modules("Module1").ProcessDatainstead of justModule1.ProcessData.
Tips
- **Use the "Project Explorer" (Ctrl+R)** to verify module names—right-click the module and check its path to confirm it’s loaded in the current workbook.
-
**For large workbooks, rename modules** to avoid path-related errors (e.g.,
MyUtilities.Module1). - **Debug with breakpoints**—set a breakpoint in the private procedure and trigger the call to ensure execution flows correctly.
-
**Leverage dynamic modules**—if modules change frequently, store them in a separate file and reference them dynamically:
Dim myModule As Object Set myModule = ThisWorkbook.Modules("File1") myModule.ProcessData Range("A1")
Troubleshooting
-
**"Sub not found" error**:
- Check the module name spelling (case-sensitive in some scenarios).
- Ensure the module is loaded in the current workbook (open
Project Explorerand verify it’s not blank). - If using a separate project file, add a reference to the workbook containing the module.
-
**Access violation (runtime error 9)**:
- Passing invalid objects (e.g.,
Nothinginstead of aRange) can crash. Always validate inputs. - Check for typos in procedure/parameter names.
- Passing invalid objects (e.g.,
-
**Module not resolving in a new workbook**:
- Copy the module code into the new workbook (delete the old one first).
- If using a separate file, ensure it’s linked via
ThisWorkbook.Modules.
Frequently Asked Questions
1. How do I call a private procedure from a userform?
Use the same syntax as in modules. For example, in your userform’s code:
Module1.ProcessData Me.txtInput.Value
Replace Me with the object’s name if using a different control. Ensure the module is accessible to the userform (e.g., placed in the same workbook).
2. Can I call a private procedure from a worksheet event?
Yes, but you must resolve the module dynamically if it’s not in the global scope. Use:
ThisWorkbook.Sheets("Sheet1").PrivateSubName
Note: Excel’s worksheet events often lack access to private modules unless the procedure is declared Public.
3. What if the module is in a VBA project file (not workbook)?
Add a reference to the workbook containing the module:
Dim myModule As Object
Set myModule = ThisWorkbook.Modules("MyModule")
myModule.PrivateProc(data)
If the module is in a separate .vba project file, use:
Set myModule = ThisWorkbook.VBProject.Modules("MyModule")
4. How do I debug a private procedure called from another module?
Set a breakpoint inside the private procedure, then trigger the call. If it fails, check:
- The module is loaded in the workbook.
- No typos in the procedure name.
- The parameters match exactly (e.g.,
Rangevs.String).
5. Can I make a private procedure "public" for a specific caller?
No, scoping is absolute. Instead, use a Public helper method that invokes the private procedure:
' In Module1
Private Sub ProcessDataPrivate(input As Range)
Debug.Print input.Address
End Sub
' Public wrapper in Module2
Public Sub ProcessDataPublic(input As Range)
Module1.ProcessDataPrivate input
End Sub
6. Why won’t my private procedure run when called from a macro?
Likely because the module isn’t in the workbook’s scope. Solutions:
- Move the module code to the workbook.
- Use dynamic resolution (see FAQ #3).
- Declare the module as
Publicif isolation isn’t critical.
Need further help? Check that both modules belong to the same workbook, and verify the procedure name matches exactly in both locations.