Lifetime of variable

tiredofit

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

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

steve the fish

Well-known Member
Joined
Oct 20, 2009
Messages
8,536
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
37,146
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,136,864
Messages
5,678,210
Members
419,751
Latest member
richkings

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