Link to home
Start Free TrialLog in
Avatar of Saqib Husain
Saqib HusainFlag for Pakistan

asked on

VBA Uninitializing a variable

How do I uninitialize a variable so that the isempty() function returns a true? The variable is a variant.
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

I think the best approach would not to use a Variant. How is your variable being used?
Avatar of Saqib Husain

ASKER

As a double
Actually I have this bad habit of not declaring variables. I often change types
You can still change types if you explicitly type a variable, and Variants are the least efficient and largest variable type so you might want to change your "bad habit".
ASKER CERTIFIED SOLUTION
Avatar of IrogSinta
IrogSinta
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Norie
Norie

You really should declare all variables,  not doing so can lead to problems, eg unexpected results,  hard to debug code etc

If you add Option Explicit at the top of each module you'll be forced to declare all variables.

PS You can use Tools>Options... Require Variable Declaration in the VBE to automatically add Option Explicit to every module.
you can have a variable that you keep without assigning values to it, and when you want to uninitialized any other variables just assign that variable to it
like this
Dim Vartest As Variant
Dim xEmptyVar As Variant
Vartest = "SomeData"
Vartest = xEmptyVar
Debug.Print IsEmpty(Vartest)

Open in new window

Martin, you showed me how to uninitialize it using     = Empty

I used it and it works just fine. But your comment is now gone.
Actually I showed you here that you can do that.  Look at the 5th post.

Ron