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., Module1 and Module2)
  • 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+F11 to access it)

Step-by-Step

  1. **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, in Module1, add a private sub like this:
        Private Sub ProcessData(inputRange As Range)
            Debug.Print "Processing data from: " & inputRange.Address
            ' Add your logic here
        
    ProcessData is now private—only accessible within Module1.
  2. **Reference the Module Where the Procedure Resides**. From the procedure calling the private sub (e.g., Module2 or 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 myDataRange
        
    Key points:
    • Use ModuleName.ProcedureName to bypass "private" restrictions.
    • If the module is in a different project (like a separate VBA file), prefix with ThisWorkbook.Namespace.Name or the project’s module path.
    Name resolution issue: If Excel complains about "Module not found," ensure:
    1. The module is actually defined in the workbook.
    2. You’re not using the wrong project scope (e.g., ThisWorkbook.Sheets instead of Module1).
  3. **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"))
        
  4. **Trigger the Call from a User Event**. Attach the procedure call to an event (e.g., button click, worksheet change) in the VBA editor:
    1. Double-click a button in the Excel workbook to open the code editor.
    2. Press F8 to insert a call to Module1.ProcessData (e.g., after Worksheet_Change or Button_Click).
    Example event code:
        Private Sub Button1_Click()
            Module1.ProcessData ThisWorkbook.Sheets("Sheet1").Range("D1:D10")
        

Common Mistakes

  1. **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).
  2. **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.
  3. **Using Public Sub instead of Private Sub**—this exposes the procedure globally, defeating the purpose of modularity.
  4. **Ignoring scope errors**—if the module is in a separate project file, reference it via ThisWorkbook.Modules("Module1").ProcessData instead of just Module1.ProcessData.

Tips

  1. **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.
  2. **For large workbooks, rename modules** to avoid path-related errors (e.g., MyUtilities.Module1).
  3. **Debug with breakpoints**—set a breakpoint in the private procedure and trigger the call to ensure execution flows correctly.
  4. **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

  1. **"Sub not found" error**:
    1. Check the module name spelling (case-sensitive in some scenarios).
    2. Ensure the module is loaded in the current workbook (open Project Explorer and verify it’s not blank).
    3. If using a separate project file, add a reference to the workbook containing the module.
  2. **Access violation (runtime error 9)**:
    1. Passing invalid objects (e.g., Nothing instead of a Range) can crash. Always validate inputs.
    2. Check for typos in procedure/parameter names.
  3. **Module not resolving in a new workbook**:
    1. Copy the module code into the new workbook (delete the old one first).
    2. 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., Range vs. 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 Public if 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.