• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4553
  • Last Modified:

How can I check if a VBA variable exist?

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
abfinfo
Asked:
abfinfo
2 Solutions
 
David LeeCommented:
Hi, abfinfo.

What do you mean by "exist"?  Does that mean declared, has a value, or something else?
0
 
abfinfoAuthor Commented:
I want a check if the var is declared and if not i declare it.
0
 
David LeeCommented:
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
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
abfinfoAuthor Commented:
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
 
David LeeCommented:
"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
 
wobbledCommented:
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
 
abfinfoAuthor Commented:
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
 
abfinfoAuthor Commented:
Thanks again for your time and help!

Best regards,
abfinfo
0
 
David LeeCommented:
You're welcome.
0
 
coeurvertCommented:
The vartype() function returns 0 if the variable has not been declared.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now