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.
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
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
-
A CVE-MITRE-CISA-CNA Extravaganza
by
Nibbled To Death By Ducks
5 hours, 42 minutes ago -
Sometimes I wonder about these bots
by
Susan Bradley
1 hour, 59 minutes ago -
Does windows update component store “self heal”?
by
Mike Cross
18 hours, 39 minutes ago -
Windows 11 Insider Preview build 27858 released to Canary
by
joep517
19 hours, 39 minutes ago -
Pwn2Own Berlin 2025: Day One Results
by
Alex5723
19 hours, 4 minutes ago -
Windows 10 might repeatedly display the BitLocker recovery screen at startup
by
Susan Bradley
15 hours, 34 minutes ago -
Windows 11 Insider Preview Build 22631.5409 (23H2) released to Release Preview
by
joep517
22 hours, 21 minutes ago -
Windows 10 Build 19045.5912 (22H2) to Release Preview Channel
by
joep517
22 hours, 22 minutes ago -
Kevin Beaumont on Microsoft Recall
by
Susan Bradley
10 hours, 56 minutes ago -
The Surface Laptop Studio 2 is no longer being manufactured
by
Alex5723
1 day, 6 hours ago -
0Patch, where to begin
by
cassel23
1 day ago -
CFPB Quietly Kills Rule to Shield Americans From Data Brokers
by
Alex5723
1 day, 20 hours ago -
89 million Steam account details just got leaked,
by
Alex5723
1 day, 7 hours ago -
KB5058405: Linux – Windows dual boot SBAT bug, resolved with May 2025 update
by
Alex5723
2 days, 4 hours ago -
A Validation (were one needed) of Prudent Patching
by
Nibbled To Death By Ducks
1 day, 19 hours ago -
Master Patch Listing for May 13, 2025
by
Susan Bradley
1 day, 6 hours ago -
Installer program can’t read my registry
by
Peobody
13 minutes ago -
How to keep Outlook (new) in off position for Windows 11
by
EspressoWillie
1 day, 17 hours ago -
Intel : CVE-2024-45332, CVE-2024-43420, CVE-2025-20623
by
Alex5723
2 days ago -
False error message from eMClient
by
WSSebastian42
2 days, 15 hours ago -
Awoke to a rebooted Mac (crashed?)
by
rebop2020
3 days ago -
Office 2021 Perpetual for Mac
by
rebop2020
3 days, 2 hours ago -
AutoSave is for Microsoft, not for you
by
Will Fastie
4 hours, 40 minutes ago -
Difface : Reconstruction of 3D Human Facial Images from DNA Sequence
by
Alex5723
3 days, 5 hours ago -
Seven things we learned from WhatsApp vs. NSO Group spyware lawsuit
by
Alex5723
13 hours, 2 minutes ago -
Outdated Laptop
by
jdamkeene
3 days, 11 hours ago -
Updating Keepass2Android
by
CBFPD-Chief115
3 days, 16 hours ago -
Another big Microsoft layoff
by
Charlie
3 days, 16 hours ago -
PowerShell to detect NPU – Testers Needed
by
RetiredGeek
18 hours, 16 minutes ago -
May 2025 updates are out
by
Susan Bradley
1 hour, 30 minutes 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.