Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8976873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:14:39+00:00 2026-06-15T19:14:39+00:00

As my first language and as completely taught from other’s example I never questioned

  • 0

As my first language and as completely taught from other’s example I never questioned the standard practice in VBA of grouping all variable declarations at the start of the module, routine or function they are scoped to as in this example.

Sub Traditional()
Dim bVariable as Boolean
Dim OtherVariable
' Some code using OtherVariable goes here
'
' Now we use bVariable
bVariable = True
Do While bVariable
    bVariable = SomeFunction()
Loop
End Sub

Now I’m learning that standard practice in other languages is to declare variables as close to where they are used as possible, like this:

Sub Defensive()
Dim OtherVariable as String
' Some code using OtherVariable goes here
'
' Now we use bVariable
Dim bVariable as Boolean
bVariable = True
Do While bVariable
    bVariable = SomeFunction()
Loop
End Sub

This seems completely sensible to me as a defensive programming practice – in that it limits both span and live time (as explained in Code Complete), so I’m wondering if there is any reason for not doing the same in VBA? Possible reasons I can think of are memory, running time (e.g. repeatedly declaring inside a loop), tradition – arguably a good reason as there must be hundreds of thousands of VBA programmers who expect to see all used variables at the start of the routine. Are there any I’ve missed that might explain the benefit of this practice or at least where it came from?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:14:40+00:00Added an answer on June 15, 2026 at 7:14 pm

    I declare all my variables at the top. I think declaring them closer to first use masks (at least) two other problems that should be fixed first.

    1. Procedures too long: If you’re procedure is more than fits on a screen, perhaps it’s doing too much and should be broken into smaller chunks. You’ll also find that unit tests are way easier to write when your procedures are small and only do one thing.
    2. Too many variables: If you have a bunch of related variables, consider using a custom class module or user-defined type. It will make the code more readable and easier to maintain.

    If your procedures are short and you’re using classes and UDTs, the benefits of declaring the variables at the point of use are lessened or eliminated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First of all, my ubiquitous language is PHP, and I'm thinking about learning Java.
Writing my first C# application...never touched the language before and not much of a
First of all, I understand in 90% of applications the performance difference is completely
First of all, there are other topics covering this subject but they were posted
First of all, I am not a veteran programmer i any language. But I've
English isn't my first language so i can't come up with a good name.
C++ was the the first language I've learnt so dividing source code into .h
I have been programming in Java (my first language) for about six months and
I am new to programming and am learning Python as my first language. I
sorry in advance for any orthographic mistakes (english is not my first language). I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.