This has probably been discussed before, but in the absence of a search function, what are the reasons for declaring all your VBA variables. It seems unnecessarily cumbersome to be writing code and then have to jump to the top every time you introduce a new variable. The only times where it seems necessary are for arrays and I’ve also had occasions where my code only worked when I declared something a Double.
![]() |
There are isolated problems with current patches, but they are well-known and documented on this site. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
Declaring VBA Variables (Excel VBA)
Home » Forums » AskWoody support » Productivity software by function » MS Excel and spreadsheet help » Declaring VBA Variables (Excel VBA)
- This topic has 26 replies, 6 voices, and was last updated 21 years, 7 months ago.
AuthorTopicWSchipshot
AskWoody LoungerOctober 10, 2003 at 12:52 pm #394853Viewing 5 reply threadsAuthorReplies-
WSAlexya1
AskWoody Lounger -
WSHoward Kaikow
AskWoody LoungerOctober 10, 2003 at 8:47 pm #727340Yes, but there are two caveats:
1. VB is actually an interpreter, not a compiler, so all lines do not necessarily even get “used” at run-time.
It is safer to put all declarations at the beginning to avoid such problems.2. In VB .NET, declarations will be able to have local scope, so declaring locally in VBA /VB might have difficult side-effects when upgrading to VB.NET.
-
WSAlexya1
AskWoody LoungerOctober 12, 2003 at 11:23 pm #727868I agree that there is good reason to declare (and initialize) at the top of the procedure, function or module, however I can’t say that I’ve heard either of those reasons before…
When you say VB is not a compiler… that confuses me… The language we choose to write code in is never the compiler itself… but the code must be compiled in order to be used… How could it be used if it wasn’t compiled into machine language?… I’d love to hear more about what you’re saying because this is the first I’ve heard of it…
As for declarations being able to have local scope in .NET… isn’t that always the way??… Local scope (in what I was taught) is being able to declare and use variables or objects of a class in a procedure or function and have them not available to any other procedure, function or module in the project… Procedure, module and global level declarations have always defined the scope from what I know… What are you referring to??
Sorry… but I’m confused…. Thanks Howard…
-
WSHoward Kaikow
AskWoody LoungerOctober 13, 2003 at 4:01 am #727908An interpreter, usually, only looks at the lines of code needed to execute.
A compiler processes all lines of code.“local” scope is different in VB .NET.
For example, you could declare a variable that had the scope of only a particular For .. Next loop.
In VB/VBA, a variable has at least th scope of the procedure in which it is declared. -
WSHoward Kaikow
AskWoody LoungerOctober 13, 2003 at 4:01 am #727909An interpreter, usually, only looks at the lines of code needed to execute.
A compiler processes all lines of code.“local” scope is different in VB .NET.
For example, you could declare a variable that had the scope of only a particular For .. Next loop.
In VB/VBA, a variable has at least th scope of the procedure in which it is declared. -
WSAlanMiller
AskWoody LoungerOctober 13, 2003 at 2:24 pm #728083[indent]
When you say VB is not a compiler… that confuses me… The language we choose to write code in is never the compiler itself… but the code must be compiled in order to be used… How could it be used if it wasn’t compiled into machine language?… I’d love to hear more about what you’re saying because this is the first I’ve heard of it…
[/indent]
The .exe produced from the VB “compiler” is actually NOT the sort of machine code produced by compiling an application using, say, a C/C++ compiler. It’s called P-Code I think, and is more akin to the “bytecode” produced when “compiling” a (cross platform) Java executable. In order for it to run, it’s necessary for runtime modules (MSVBVM60, JRE etc.) to convert this pseudo-compiled code into real machine code i.e. interpret it at runtime. C++, Delphi, Pascal, and Assembly are genuine compiled languages, and are regarded as rather more efficient/ higher performance for this reason. As for the way it’s done in VBA, where there is no obvious “executable” involved, I’d be interested in this too. I imagine code would be even less compiledthan for a VB app.
Alan
Edited – Take a look at http://www.programmersheaven.com/articles/…s/john/vbvm.htm%5B/url%5D for a far better and more detailed explanation than the above. There’s even an addin there to let you watch the action as your code runs… looks like fun!
-
WSAlanMiller
AskWoody LoungerOctober 13, 2003 at 2:24 pm #728084[indent]
When you say VB is not a compiler… that confuses me… The language we choose to write code in is never the compiler itself… but the code must be compiled in order to be used… How could it be used if it wasn’t compiled into machine language?… I’d love to hear more about what you’re saying because this is the first I’ve heard of it…
[/indent]
The .exe produced from the VB “compiler” is actually NOT the sort of machine code produced by compiling an application using, say, a C/C++ compiler. It’s called P-Code I think, and is more akin to the “bytecode” produced when “compiling” a (cross platform) Java executable. In order for it to run, it’s necessary for runtime modules (MSVBVM60, JRE etc.) to convert this pseudo-compiled code into real machine code i.e. interpret it at runtime. C++, Delphi, Pascal, and Assembly are genuine compiled languages, and are regarded as rather more efficient/ higher performance for this reason. As for the way it’s done in VBA, where there is no obvious “executable” involved, I’d be interested in this too. I imagine code would be even less compiledthan for a VB app.
Alan
Edited – Take a look at http://www.programmersheaven.com/articles/…s/john/vbvm.htm%5B/url%5D for a far better and more detailed explanation than the above. There’s even an addin there to let you watch the action as your code runs… looks like fun!
-
-
WSAlexya1
AskWoody LoungerOctober 12, 2003 at 11:23 pm #727869I agree that there is good reason to declare (and initialize) at the top of the procedure, function or module, however I can’t say that I’ve heard either of those reasons before…
When you say VB is not a compiler… that confuses me… The language we choose to write code in is never the compiler itself… but the code must be compiled in order to be used… How could it be used if it wasn’t compiled into machine language?… I’d love to hear more about what you’re saying because this is the first I’ve heard of it…
As for declarations being able to have local scope in .NET… isn’t that always the way??… Local scope (in what I was taught) is being able to declare and use variables or objects of a class in a procedure or function and have them not available to any other procedure, function or module in the project… Procedure, module and global level declarations have always defined the scope from what I know… What are you referring to??
Sorry… but I’m confused…. Thanks Howard…
-
-
WSHoward Kaikow
AskWoody LoungerOctober 10, 2003 at 8:47 pm #727341Yes, but there are two caveats:
1. VB is actually an interpreter, not a compiler, so all lines do not necessarily even get “used” at run-time.
It is safer to put all declarations at the beginning to avoid such problems.2. In VB .NET, declarations will be able to have local scope, so declaring locally in VBA /VB might have difficult side-effects when upgrading to VB.NET.
-
-
WSAlexya1
AskWoody Lounger -
WSAlexya1
AskWoody LoungerOctober 10, 2003 at 1:35 pm #727138Check out this link…. It explains the three main reasons for declaring your variables much more eloquently than I can…
http://www.cpearson.com/excel/DeclaringVariables.htm%5B/url%5DPersonally… as a programmer analyst… I would always declare my variables… One of the first things that we are taught in school (for programming) is that you don’t just sit down and start writing your code… You should decide what variables you need, their data types and uses long before you write one line of code… If you are having to “jump to the top” during coding, you probably haven’t done enough analysis ahead of time… On the fly coding can cause serious headaches later on…
Just my two cents…
-
WSshades
AskWoody LoungerOctober 10, 2003 at 3:01 pm #727212I have been teaching myself Excel VBA and came across this suggestion about forcing the programmer to declare variables in Walkenbach’s book (I think). Anyway, I followed that guideline by changing the options. The first week I was frustrated because it seemed like I was stopped at every line! What I soon discovered is that I had developed some bad habits. Now I no longer look at this Option Explicit line as an intrusion but as a tremendous aid in writing code.
-
WSshades
AskWoody LoungerOctober 10, 2003 at 3:01 pm #727213I have been teaching myself Excel VBA and came across this suggestion about forcing the programmer to declare variables in Walkenbach’s book (I think). Anyway, I followed that guideline by changing the options. The first week I was frustrated because it seemed like I was stopped at every line! What I soon discovered is that I had developed some bad habits. Now I no longer look at this Option Explicit line as an intrusion but as a tremendous aid in writing code.
-
WSHoward Kaikow
AskWoody Lounger -
WSAlexya1
AskWoody LoungerOctober 12, 2003 at 11:11 pm #727866Howard’s right….
It seems to be rare in VB to get “garbage” values from variables that aren’t initialized before use, but in C++ it’s common, as I’m sure it is in other languages as well… If you are going to write code, then it’s best to adopt the best possible practices from the very start… You’ll have enough time to develop bad habits later…
-
WSAlexya1
AskWoody LoungerOctober 12, 2003 at 11:11 pm #727867Howard’s right….
It seems to be rare in VB to get “garbage” values from variables that aren’t initialized before use, but in C++ it’s common, as I’m sure it is in other languages as well… If you are going to write code, then it’s best to adopt the best possible practices from the very start… You’ll have enough time to develop bad habits later…
-
-
WSHoward Kaikow
AskWoody Lounger
-
-
WSAlexya1
AskWoody LoungerOctober 10, 2003 at 1:35 pm #727139Check out this link…. It explains the three main reasons for declaring your variables much more eloquently than I can…
http://www.cpearson.com/excel/DeclaringVariables.htm%5B/url%5DPersonally… as a programmer analyst… I would always declare my variables… One of the first things that we are taught in school (for programming) is that you don’t just sit down and start writing your code… You should decide what variables you need, their data types and uses long before you write one line of code… If you are having to “jump to the top” during coding, you probably haven’t done enough analysis ahead of time… On the fly coding can cause serious headaches later on…
Just my two cents…
-
WSchipshot
AskWoody LoungerOctober 14, 2003 at 4:10 pm #728601Well, believe it or not, I have been following this discussion. While it seems to have drifted a little off topic, I believe I understand the arguments for declaring variables. I do have some computer science in my academic background, but I have never been a programmer. I’m the kind of guy that everyone else comes to for Excel help. Since I don’t normally create “applications” with dozens and dozens of lines of code which are hard to debug, I consider the arguments in favor of Option Explicit to be a bit esoteric and the benefits minimal. I think I write well-organized code and I don’t see how declaring variables would improve that. As one poster said, a real programmer would know all the needed variables before writing a single line of code. That may be true, but for my scale of project, I don’t think that kind of upfront analysis is time well-spent. The next time I embark on something with a larger scale, I’ll declare my variables. For my usual utility macros for PERSONAL.XLS or custom functions, I think I’ll continue in my slothful ways.
I would add another benefit to declaring variables: you automatically get your intended cApiTaliZATIon each time you use the variable.
Thanks to everyone for your input.
-
WSunkamunka
AskWoody LoungerOctober 14, 2003 at 5:20 pm #728622Not declaring variables slows your code down. Undeclared variables get treated as variants; which take up more space in memory.
Even the “simplest” code becomes hard to maintain/update/adapt – either by the author or (especially) by another user – when the variables are not declared and (from the sound of it) the code is not commented either.
-
WSunkamunka
AskWoody LoungerOctober 14, 2003 at 5:20 pm #728623Not declaring variables slows your code down. Undeclared variables get treated as variants; which take up more space in memory.
Even the “simplest” code becomes hard to maintain/update/adapt – either by the author or (especially) by another user – when the variables are not declared and (from the sound of it) the code is not commented either.
-
-
WSchipshot
AskWoody LoungerOctober 14, 2003 at 4:10 pm #728602Well, believe it or not, I have been following this discussion. While it seems to have drifted a little off topic, I believe I understand the arguments for declaring variables. I do have some computer science in my academic background, but I have never been a programmer. I’m the kind of guy that everyone else comes to for Excel help. Since I don’t normally create “applications” with dozens and dozens of lines of code which are hard to debug, I consider the arguments in favor of Option Explicit to be a bit esoteric and the benefits minimal. I think I write well-organized code and I don’t see how declaring variables would improve that. As one poster said, a real programmer would know all the needed variables before writing a single line of code. That may be true, but for my scale of project, I don’t think that kind of upfront analysis is time well-spent. The next time I embark on something with a larger scale, I’ll declare my variables. For my usual utility macros for PERSONAL.XLS or custom functions, I think I’ll continue in my slothful ways.
I would add another benefit to declaring variables: you automatically get your intended cApiTaliZATIon each time you use the variable.
Thanks to everyone for your input.
Viewing 5 reply threads -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
Frustrating search behavior with Outlook
by
MrJimPhelps
1 hour, 48 minutes ago -
June 2025 Office non-Security Updates
by
PKCano
5 hours, 1 minute ago -
Secure Boot Update Fails after KB5058405 Installed
by
SteveIT
22 minutes ago -
Firefox Red Panda Fun Stuff
by
Lars220
4 hours, 57 minutes ago -
How start headers and page numbers on page 3?
by
Davidhs
15 hours, 21 minutes ago -
Attack on LexisNexis Risk Solutions exposes data on 300k +
by
Nibbled To Death By Ducks
9 hours, 57 minutes ago -
Windows 11 Insider Preview build 26200.5622 released to DEV
by
joep517
1 day ago -
Windows 11 Insider Preview build 26120.4230 (24H2) released to BETA
by
joep517
1 day ago -
MS Excel 2019 Now Prompts to Back Up With OneDrive
by
lmacri
13 hours, 45 minutes ago -
Firefox 139
by
Charlie
6 hours, 20 minutes ago -
Who knows what?
by
Will Fastie
53 minutes ago -
My top ten underappreciated features in Office
by
Peter Deegan
1 day ago -
WAU Manager — It’s your computer, you are in charge!
by
Deanna McElveen
19 hours, 11 minutes ago -
Misbehaving devices
by
Susan Bradley
1 day, 2 hours ago -
.NET 8.0 Desktop Runtime (v8.0.16) – Windows x86 Installer
by
WSmeyerbos
2 days, 6 hours ago -
Neowin poll : What do you plan to do on Windows 10 EOS
by
Alex5723
4 hours, 4 minutes ago -
May 31, 2025—KB5062170 (OS Builds 22621.5415 and 22631.5415 Out-of-band
by
Alex5723
2 days, 5 hours ago -
Discover the Best AI Tools for Everything
by
Alex5723
1 day, 4 hours ago -
Edge Seems To Be Gaining Weight
by
bbearren
1 day, 19 hours ago -
Rufus is available from the MSFT Store
by
PL1
2 days, 3 hours ago -
Microsoft : Ending USB-C® Port Confusion
by
Alex5723
3 days, 6 hours ago -
KB5061768 update for Intel vPro processor
by
drmark
1 day, 6 hours ago -
Outlook 365 classic has exhausted all shared resources
by
drmark
1 day, 5 hours ago -
My Simple Word 2010 Macro Is Not Working
by
mbennett555
3 days, 2 hours ago -
Office gets current release
by
Susan Bradley
3 days, 4 hours ago -
FBI: Still Using One of These Old Routers? It’s Vulnerable to Hackers
by
Alex5723
4 days, 18 hours ago -
Windows AI Local Only no NPU required!
by
RetiredGeek
4 days, 3 hours ago -
Stop the OneDrive defaults
by
CWBillow
4 days, 19 hours ago -
Windows 11 Insider Preview build 27868 released to Canary
by
joep517
5 days, 5 hours ago -
X Suspends Encrypted DMs
by
Alex5723
5 days, 7 hours ago
Recent blog posts
Key Links
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.