top of page
Writer's pictureAntriksh Sharma

Option Private Module in VBA - Prevent users from accessing sub-routine or functions.

VBA allows you to create sub-routines or functions that can be hidden from the end user using only Excel interface, but sometimes that option isn't the desired one, mostly because when you declare a procedure or a function with Private keyword, it prevents the developer from accessing it from other modules in the VBE, but luckily there is a keyword that can come to your rescue, let's see how:


First we need to create 2 sub-routines, one returns a simple message and other one acts as a calling procedure

When I run the IncrementNumbers_Caller procedure it gives me the message shown in the below image:

So far so good, now I have modified the declaration of the first procedure slightly by adding the Private keyword

If I go back into Excel and Press ALT + F8 then you can see that the IncrementNumbers() sub-routine is no longer visible, which is a good thing as we don't want the end user to have direct access to it.

The problem arrises when one tries to reference that sub-routine in an another module and since it has been declared as Private, its scope is only limited to the module it resides in.


The below image shows that the sub-routine isn't accessible in a different module.

To resolve this issue what we can do is make a declaration at the top of the module and write Option Private Module

Now if I try to access the procedure from another module I am able to do so.

And if I try to access it from within Excel, it doesn't show up!


603 views3 comments

Recent Posts

See All

3 Comments


Edwin Broeks
Edwin Broeks
Sep 12, 2023

There is a way to call a Private Sub, by using the following command line: Run "Name of Sub"

Like

Rajeev Negi
Rajeev Negi
May 27, 2021

So "Option Private Module" makes the Private Subroutine Public in aspect of VBA only.

Like
antrikshblogs
May 28, 2021
Replying to

Correct!

Like
bottom of page