Lifetime of variable

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,832
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

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
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,214,393
Messages
6,119,261
Members
448,880
Latest member
aveternik

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