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, 8 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
-
Totally disable Bitlocker
by
CWBillow
39 minutes ago -
Totally disable Bitlocker
by
CWBillow
2 hours, 18 minutes ago -
Windows 11 ad from Campaign Manager in Windows 10 (Awaiting moderation)
by
Jim McKenna
5 hours, 47 minutes ago -
Phishers extract Millions from HMRC accounts..
by
Microfix
14 hours, 26 minutes ago -
Windows 10 22H2 Update today (5 June) says up-to-date but last was 2025-04
by
Alan_uk
16 hours, 18 minutes ago -
Thoughts on Malwarebytes Scam Guard for Mobile?
by
opti1
19 hours, 27 minutes ago -
Mystical Desktop
by
CWBillow
19 hours, 40 minutes ago -
Meta and Yandex secretly tracked billions of Android users
by
Alex5723
58 minutes ago -
MS-DEFCON 2: Do you need that update?
by
Susan Bradley
9 hours, 51 minutes ago -
CD/DVD drive is no longer recognized
by
WSCape Sand
1 day, 10 hours ago -
Windows 11 24H2 Default Apps stuck on Edge and Adobe Photoshop
by
MikeBravo
1 day, 13 hours ago -
North Face and Cartier customer data stolen in cyber attacks
by
Alex5723
1 day, 11 hours ago -
What is wrong with simple approach?
by
WSSpoke36
1 day, 4 hours ago -
Microsoft-Backed Builder.ai Set for Bankruptcy After Cash Seized
by
Alex5723
1 day, 23 hours ago -
Location, location, location
by
Susan Bradley
13 hours, 39 minutes ago -
Cannot get a task to run a restore point
by
CWBillow
2 days ago -
Frustrating search behavior with Outlook
by
MrJimPhelps
1 day, 15 hours ago -
June 2025 Office non-Security Updates
by
PKCano
2 days, 11 hours ago -
Secure Boot Update Fails after KB5058405 Installed
by
SteveIT
13 hours, 59 minutes ago -
Firefox Red Panda Fun Stuff
by
Lars220
2 days, 11 hours ago -
How start headers and page numbers on page 3?
by
Davidhs
2 days, 21 hours ago -
Attack on LexisNexis Risk Solutions exposes data on 300k +
by
Nibbled To Death By Ducks
2 days ago -
Windows 11 Insider Preview build 26200.5622 released to DEV
by
joep517
3 days, 6 hours ago -
Windows 11 Insider Preview build 26120.4230 (24H2) released to BETA
by
joep517
3 days, 6 hours ago -
MS Excel 2019 Now Prompts to Back Up With OneDrive
by
lmacri
2 days, 20 hours ago -
Firefox 139
by
Charlie
2 days, 12 hours ago -
Who knows what?
by
Will Fastie
1 day, 15 hours ago -
My top ten underappreciated features in Office
by
Peter Deegan
3 days, 7 hours ago -
WAU Manager — It’s your computer, you are in charge!
by
Deanna McElveen
1 day ago -
Misbehaving devices
by
Susan Bradley
2 days, 2 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.