Solved

How can I check if a VBA variable exist?

Posted on 2009-04-08
10
2,972 Views
Last Modified: 2012-06-21
Hi,

Can you please tell me how can I check if a VBA variable (not a Word property) exist? Please post some sample code.

Best regards,
Abfinfo
0
Comment
Question by:abfinfo
10 Comments
 
LVL 76

Expert Comment

by:David Lee
ID: 24098935
Hi, abfinfo.

What do you mean by "exist"?  Does that mean declared, has a value, or something else?
0
 

Author Comment

by:abfinfo
ID: 24098980
I want a check if the var is declared and if not i declare it.
0
 
LVL 76

Expert Comment

by:David Lee
ID: 24099107
There's no need to.  The point of declaring a variable is to give it a type and reserve space for it before the code runs.  Once the code is running, then VBA use automatic declaration.  In other words any reference to a variable that doesn't already exist will create it.  Take the following snippet for example

    Dim myName, myAddress
    myName = "BlueDevilFan"
    myAddress = "Experts-Exchange"
    myPhone = "555-1212"

Even though I didn't declare myPhone the act of assigning a value to it will create it.  The same applies if I reference it at all.

    MsgBox myPhone

Would create a variable named myPhone and assign a null value to it.

If you want to insure that all variables are declared before running your code, then add the command

    Option Explicit

at the top of your code module.  That command will force you to declare all variables before the code can run.
0
 

Author Comment

by:abfinfo
ID: 24105362
I know this, but I do something like:

---------->
If (Var1 = False) Then
End If
---------->

I run into problems. I don't see any solution for something like this except doing it over error handling which looks ugly to me. I know it's ugly anyway, but I need this for a special case... So is there any solution for this?

Best regards and big thanks,
abfinfo
0
 
LVL 76

Accepted Solution

by:
David Lee earned 250 total points
ID: 24105594
"I know this, but I do something like:

---------->
If (Var1 = False) Then
End If
---------->

I run into problems."

I expect the "problem" you run into is that the statement evaluates to False making it appear to the code as if the variable was declared when in fact it isn't.  That's because referencing the previously non-existent variable Var1 creates it.  VB/VBA does not have a command or function to test for variable existence.  So, we'd have to devise a test.  How will it be possible to test for a variable when any code that references a non-existent variable will create the variable?  

I expect there's no test because the folks who created the language never envisioned the need to test for a variable's existence.  What purpose does it serve?   How would you use it?  Will you prefix each reference to a variable with a test to see if if exists?  Wouldn't that complicate the code and make it impossibly cumbersome?  The solution is to use Option Explicit which forces you to declare all variables at design time.  Option Explicit is specifically intended to solve this problem.  
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 17

Assisted Solution

by:wobbled
wobbled earned 250 total points
ID: 24105875
You could do

If Not Var1 Is Nothing Then ....

which would assume that the variable has not been previously declared or has no data assigned to it.  Either way not ideal as after this you will have a variable created for Var1, but its value will be null still
0
 

Author Comment

by:abfinfo
ID: 24106511
It's a litte bit hard to explain why I need this. Easily explained my problem is that I start a kind of launcher from two directions. From one direction is should run normaly, from another it should run the launcher without doing a specific thing.

I saw it was my fault I had declared the (boolean) variable, but I asked if(var1 = false) then... and this forced and error. The solution was quit simple, I had to ask if(var1 = "") this works perfect.

I think you both gave me some very useful hints to get this problem solved, so I think it's fair to split the points. I hope this is ok.
0
 

Author Closing Comment

by:abfinfo
ID: 31568112
Thanks again for your time and help!

Best regards,
abfinfo
0
 
LVL 76

Expert Comment

by:David Lee
ID: 24106543
You're welcome.
0
 

Expert Comment

by:coeurvert
ID: 34096130
The vartype() function returns 0 if the variable has not been declared.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now