VBA Private Sub and Public Sub


A VBA Private Sub can only be called from anywhere in the Module in which it resides. A Public Sub in the objects, ThisWorkbook, ThisDocument, Sheet1, etc. cannot be called from anywhere in a project.

Aside from Private and Public, you may also declare a Subroutine as Static. If you declare a Sub as Static, all the variables declared within the Sub will retain their values when the Sub is exited instead of the default behavior, which is that they become Empty and their value "out of context." Thus, their value is not preserved.

Variables that are used in a Sub are always Local to the Sub unless they are explicitly declared at the Module level. Module level variables are those declared above the first Sub procedure in a Module and they retain their value throughout the Module. This is the case where the variable has been declared with the Dim Keyword...

However, if you declare a Module level variable with the Public Keyword it can be used anywhere in the project and retains its value. 

This site is powered by Site Build It!. If you enjoy it, please check out the
Site Build It homepage
to learn more.


Return from Outlook VBA myMailItem Criteria to VBA Code Samples


Return to our Homepage