Variable Declaration General Question

stinkingcedar

New Member
Joined
May 2, 2016
Messages
23
Hey everyone,

So I just have a general question about variable declaration in vba.

If you declare a variable in a module, can it then be used in other subs/functions in that same module without declaring it again? Can it be used in other modules without declaring it again as well? I use option explicit almost exclusively, not sure if that has anything to do with it, but I was just wondering the scope in which the variables could be used under so I could possibly make my code more compact. Thank you!
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Also, does the private/public designation for sub-procedures and functions play a role in this as well?
 
Last edited:
Upvote 0
It depends on the scope.

If you declare the variable in a routine, it is only visible to that routine.
If you declare it with Dim or Private, at the top of a module, it is visible to routines in that module only.
If you declare it as Public, it is visible to all routines in the project.

The scope of the routine is irrelevant as far as the variable is concerned.
 
Upvote 0
It depends on the scope.

If you declare the variable in a routine, it is only visible to that routine.
If you declare it with Dim or Private, at the top of a module, it is visible to routines in that module only.
If you declare it as Public, it is visible to all routines in the project.

The scope of the routine is irrelevant as far as the variable is concerned.

So normally I declare all my variables with a Dim statement at the top of each module, what would declaring the variables in the other three ways that you mention look like syntax-wise?
 
Last edited:
Upvote 0
The first two look the same, it's just a question of where the declaration line is - at the top of a module above all routines, or inside a routine. Public variables are declared:
Code:
Public someVariable as string
for example at the top of the module before any routines.
 
Upvote 0
What if I were to set a variable, such as a worksheet variable, to a sheet in the top of the module? Would I then not have to do it in each individual procedure in that module?
 
Upvote 0
You can't assign a value to a variable outside a routine. You can only declare them.
 
Upvote 0

Forum statistics

Threads
1,215,372
Messages
6,124,531
Members
449,169
Latest member
mm424

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top