Global Variables across multiple subs

scottleger4

New Member
Joined
Oct 3, 2016
Messages
41
Office Version
  1. 365
Platform
  1. Windows
I'm having trouble with dimensioning variables to be used across multiple subs. I'm trying to keep the length of the individual subs as short as possible, and currently the only working version requires me to run the dims in every sub. In its simplest form...

Sub Master
RunSub1
RunSub2
End Sub

Sub RunSub1
Dim Variable1 As String
Dim Variable1Entry as Range
Set Variable1Entry = Sheet.Range("A1")
End Sub

Sub RunSub2
Do Until Variable1Entry.Value = ""
...
...
...
Set Variable1Entry = Variable1Entry.Offset(1, 0)
Loop
End Sub


Everything seems to work fine until VBA jumps to the "Do Until" and then debugs saying "Object Required" which I assume means the previous dim I did for that value isn't 'sticking.' There are currently about 30 variables I need to use, so copying those to the start of every sub would make my code REALLY long. I'm sure it is something pretty simple that I just can't come up with. Thoughts? Thanks in advance.
 
I do have the "Option Explicit" line at the top. Can I just get rid of that line or is it required to use the Public statements?
 
Upvote 0

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
You can get rid of it, though I would advise against doing that.

It is not required, but is a best practice, and greatly increases the chances of errors and typos in your code by requiring you to declare all your variables.

For example, let's say that you have declared a variable like this:
Dim lastRow as Long

and then later in your code, you go to set its value like this:
lsatRow = Cells(Rows.Count, "A").End(xlUp).Row

When you compile the code, or go to run it, Excel will tell you that you have an undeclared variable named "lsatRow". So it finds that typo for you right away.
If you did not have that, there would be no errors in your code, but your code might not give you the results you expect (because of the typo). So, you could waste a lot more time trying to figure out why it isn't working.

Likewise, by declaring your variables, you help ensure that incorrect data values aren't entered.
For example, let's say that you declared your variable above, and then tried this:
lastRow = "Test"

That will result in an error, as you cannot place text in a numeric variable, as opposed to accepting it and getting unwanted results.

Lastly, declaring variables save memory and increases speed, as undeclared variables are given the Variant type, which uses the most memory.

So requiring variables to be declared may take a little longer in writing the code, but can save you a lot of headaches down the road.
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,262
Members
452,627
Latest member
KitkatToby

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