VBA Public Variable – Use a Variable in Multiple Modules or Subs

Associated Files Download Links


Public Variables

It seems logical that there should be a way to assign a variable in one module and have access to it from another. The trick is to declare it as Public outside of a sub or function.

To use a variable in more than the current sub or module, you must set it’s scope to Public, and declare it outside of any sub or function.

This means at the top of the module (after option explicit if you use this) declare the variable there. To declare the variable as Public just replace the usual word Dim with Public.

You only need to declare Public variables once. Here’s what your possible Public declaration could look like:

VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro – A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!

alt text

Learn More!


<<Return to VBA Examples

Did you find this VBA tutorial useful? Then share it with your friends and colleagues: