Can I declare a variable for the entire project?

wageslave101

Board Regular
Joined
Jul 18, 2007
Messages
154
Cleaning up a Project and there are lots of the modules attached that constantly reuse the same variables over and over.

Can I declare these once, so that the every module can use these or must they be declared seperately?

For example every module in project starts with;

Code:
Sub Start()
Dim RepName As Variant
Dim RepLocation As Variant
...lots of others

RepName = Workbooks(ThisWorkbook.Name).Worksheets("RepData").Range("A1").Value
RepLocation = Workbooks(ThisWorkbook.Name).Worksheets("RepData").Range("A2").Value
... lots of others
End Sub

Seems somewhat redundant to me to delcare these in every single module within the project when they remain the same the entire time.

Thank you very much for your time,
WageSlave101
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Hi

Yes you could declare them as Public in the declarations section of a module (in fact you could use a separate module just to store your Public variables):

Code:
'at top of your module before any subs/functions
Public RepName As Variant
Public RepLocation As Variant

Note that Public variables will retain any values assigned to them in that Excel session (so if you want to use the variable in another sub but don't want any old value, then you need to specifically initialise the variable with a new value).
 
Upvote 0

Forum statistics

Threads
1,214,527
Messages
6,120,058
Members
448,940
Latest member
mdusw

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