Lifetime of variable

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,638
Office Version
  1. 2019
Platform
  1. Windows
To clear an object variable, you would set it to nothing.

How would you clear a non-object variable?

I ask because in the following code, the variable a is declared at the module level, so every time the sub is run, the variable's value increases.

I want the variable a to "clear" after the sub that uses it has ended.

Code:
Option Explicit

Dim a As Integer

Sub Test()

    a = a + 1

End Sub

The reason it's declared at the module level, as opposed to the procedure level, is I have other subs that use the variable a later on (not shown here).

Thanks
 
Last edited:

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.

steve the fish

Well-known Member
Joined
Oct 20, 2009
Messages
8,721
Office Version
  1. 365
Platform
  1. Windows
Sounds like to me you want to Dim the variable in each sub not at the top of the module.
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
39,032
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
The reason it's declared at the module level, as opposed to the procedure level, is I have other subs that use the variable a later on (not shown here).

If they aren't sharing the value of a, it shouldn't be a module level variable.
 

Forum statistics

Threads
1,171,060
Messages
5,873,533
Members
432,982
Latest member
meepio

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
Top