How to Run a Private Sub in VBA (3 Methods)

Here’s an overview of a private sub in VBA.

Overview of how to run a private sub in VBA


What Does Private Sub Mean in Visual Basic?

A private sub refers to a subroutine or method that is declared with the “Private” access modifier. This means that the sub can only be accessed and called within the same class or module where it is defined. Private subs are typically used for encapsulation purposes where certain procedures are intended. By making a sub private, you can ensure that it is not accidentally called or modified from outside the intended scope. By default, subs in Visual Basic are declared as private. So, if you don’t specify an access modifier, the sub will be private.


How to Run a Private Sub in VBA: 3 Easy Methods

We have a dataset of Student Name, their Department, and Obtained Marks. We will run a private sub using this table.

Sample dataset of how to run a private sub in VBA


Method 1 – Use the Application.Run Command

Steps:

  • Open the VBA window and insert a module.
  • Insert the following private subroutine code and save it.
Private Sub Find_Highest_Mark()
top_Mark = WorksheetFunction.Max(Range("D5:D14"))
MsgBox "The highest mark is: " & top_Mark
End Sub

VBA code to find highest mark from the datatable

  • In another module, place the following code where we have called the previously saved private subroutine through the Application.Run command.
Private Sub Running_Module1()
Application.Run "Module1.Find_Highest_Mark"
End Sub

VBA code to run a private sub using the application.run command

  • Run this module.
  • We will get our final result through a MsgBox.

Final output with running a private sub

Read More: How to Call a Sub in VBA in Excel


Method 2 – Use the Option Private Module Feature

Steps:

  • Open a module and insert the below code, then save it.
Option Private Module
Public Sub Find_Lowest_Mark()
Lowest_Mark = WorksheetFunction.Min(Range("D5:D14"))
MsgBox "The lowest mark is: " & Lowest_Mark
End Sub

VBA code with option private module

  • Open another module and call the previously saved private sub by using the code.
Private Sub Running_Module3()
Call Module3.Find_Lowest_Mark
End Sub

VBA code to call a private sub

  • We will get a MsgBox confirming the private sub is running properly.

Final output by running a private sub

  • We declared the contents as private. Thus, no macros will be visible in the Macro list.
  • Click the Macros option from the Developer tab.

Visiting Macros option from Developer tab

  • Inside the Macros window, you will not find any saved macros as we announced the module as private.

Macros window with saved private contents


Method 3 – Using a Dummy Variable

Steps:

  • Open a module, put the below code, and save it.
Public Sub Coloring_Highest_Mark(Optional byDummy As Byte)
top_Mark = WorksheetFunction.Max(Range("D5:D14"))
Range("D5:D14").Find(top_Mark).Interior.Color = vbYellow
End Sub

VBA code with used dummy variable

  • Insert another module and place the following code where we will call the previously saved private sub.
Private Sub Running_Module5()
Call Module5.Coloring_Highest_Mark
End Sub

VBA code to call the saved private module

  • We have successfully colored the cell indicating running a private sub from another module.

Final result with running a private sub

  • To check whether the contents of the module is private, you can go to the Macros list.

Macros window with saved private contents


Things to Remember

  • If you want to use events like Worksheet_Change and Worksheet_Open then you can use the Run command. Option Private Module and Dummy Variable methods are traditional ways of running a private sub.
  • While calling the private sub, make sure that the saved code is within the same module. Otherwise, it won’t work.
  • When running a private sub, make sure that any parameters passed to the private sub have the correct data types.

Frequently Asked Questions

Can a private sub be called from another module or class in VBA?

No, a private sub can only be called from within the same module or class where it is defined.

Why should I use the Option Private Module in VBA?

Option Private Module is used in VBA to prevent objects, variables, and subs declared in a module from being accessed by other modules.

What is a dummy variable in VBA?

A dummy variable is a parameter in VBA that is used as a placeholder or dummy value in a sub or function.


Download the Practice Workbook


Related Articles

Get FREE Advanced Excel Exercises with Solutions!

Tags:

Wasim Akram
Wasim Akram

Wasim Akram holds a BSc degree in Industrial and Production Engineering from Ahsanullah University of Science and Technology. Over the past 2 years, he has been actively contributing to the ExcelDemy project, where he has authored more than 150 articles. Now, he is working as an Excel VBA and Content Developer. He likes learning new things about Microsoft Office, especially Excel VBA, Power Query, Data Analysis, and Excel Statistics. He is also very interested in machine learning and... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo