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
Solved

How can I check if a VBA variable exist?

Posted on 2009-04-08
10
3,126 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.

856 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