Solved

How can I check if a VBA variable exist?

Posted on 2009-04-08
10
2,907 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
Comment Utility
Hi, abfinfo.

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

Author Comment

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

Expert Comment

by:David Lee
Comment Utility
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
Comment Utility
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
Comment Utility
"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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 17

Assisted Solution

by:wobbled
wobbled earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks again for your time and help!

Best regards,
abfinfo
0
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
You're welcome.
0
 

Expert Comment

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

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

744 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

16 Experts available now in Live!

Get 1:1 Help Now