Implicit Variable Declarations?

J

Joshua

Hi -
I am parsing code from the MVPs site, and trying to write my own.

It seems that VBA is very forgiving in terms of variable declaration. I see
variables appearing in "For Each" and "Set" statements that never were
declared with a "Dim" statement. Is this acceptable? Are there any problems
with this?

I assume that variables declared implicitly in this way do not survive
beyond the local (sub)procedure. Is this correct?

Thanks
Joshua
 
J

Joshua

Thanks!
Joshua

Jay Freedman said:
Hi, Joshua,

It's true that VBA allows undeclared variables, which are automatically
typed as Variant. As you guessed, their scope is the procedure in which
they're implicitly declared. Just because it's allowed, though, doesn't mean
it's a good practice.

There is an article on the site,
http://www.mvps.org/word/FAQs/MacrosVBA/DeclareVariables.htm, that explains
why declaring all variables is a Good Thing (tm). Unfortunately, the code in
the rest of the site hasn't always followed our own advice -- but we're
doing better than the VBA help, which almost never includes Dim statements
(and yes, I've complained about this more than once).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top