Lifetime of variable

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,926
Office Version
  1. 365
  2. 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

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Sounds like to me you want to Dim the variable in each sub not at the top of the module.
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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