Solved

How can I check if a VBA variable exist?

Posted on 2009-04-08
10
3,329 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.

707 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