100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

VBA Syntax and Variables

The building blocks of every macro: procedures, variable declaration with Dim, operators, and comments in VBA's readable, line-based syntax.

FoundationsBeginner9 min readJul 10, 2026
Analogies

VBA Syntax and Variables

VBA syntax is line-based and readable, descending from the BASIC family. Each statement usually occupies one line, continued onto the next with a space followed by an underscore, keywords are case-insensitive, and code is organized into procedures inside modules. Mastering Subs, variables, and operators is the foundation for writing any macro.

🏏

Cricket analogy: VBA's line-by-line syntax is like a cricket over: six deliveries, each a discrete statement bowled one at a time, combining into a structured over rather than one chaotic movement.

Procedures: Subs and Functions

A Sub procedure runs a block of code without returning a value and is declared with Sub...End Sub. A Function returns a value using Function...End Function, assigning the result to its own name. Both can take arguments in parentheses. Subs appear in the Macros dialog and can be run directly, while Functions can be called from other code or even used as custom worksheet formulas.

🏏

Cricket analogy: A Sub is like a fielder executing a run-out: it does a job and returns nothing. A Function is like a batsman who scores runs, handing back a value (the runs) to the scoreboard.

vba
Sub ShowTotal()
    ' A Sub calls a Function and displays its returned value
    Dim result As Double
    result = AddNumbers(12.5, 7.5)
    MsgBox "The total is " & result
End Sub

Function AddNumbers(a As Double, b As Double) As Double
    ' The result is assigned to the function's own name
    AddNumbers = a + b
End Function

Declaring Variables with Dim

Variables are declared with the Dim keyword, followed by a name and optionally As Type, for example Dim total As Double. Names must start with a letter, cannot contain spaces or most symbols, and cannot be reserved words. Declaring a specific type improves performance and helps catch errors, whereas an undeclared or Variant variable is flexible but slower and more memory-hungry.

🏏

Cricket analogy: Declaring a variable with a type is like assigning a specialist role: naming a player a wicketkeeper (As Double) tells everyone their job, unlike a vague all-rounder (Variant) who is flexible but never optimal.

Put Option Explicit at the very top of every module (Tools > Options > Require Variable Declaration turns this on automatically for new modules). It forces you to declare every variable, catching typos like 'totl' that would otherwise silently create a new empty Variant and cause subtle bugs.

Operators and Comments

VBA provides arithmetic operators (+, -, *, /, the backslash for integer division, Mod for remainder, and ^ for exponent), comparison operators (=, <>, <, >, <=, >=), and logical operators (And, Or, Not). String concatenation uses the ampersand operator, not +, which avoids ambiguity with addition. Comments begin with an apostrophe and are ignored at runtime, documenting the intent behind your code.

🏏

Cricket analogy: The ampersand concatenation operator is like joining figures on the scoreboard: "45" & " for 2" reads as one string, whereas using + would wrongly try to add them like a run total.

Use & for joining text, not +. While "A" + "B" happens to work for two strings, mixing + with numbers and text triggers implicit type coercion and can raise a Type Mismatch error or produce a wrong number. The & operator always means concatenation, so it is unambiguous.

  • VBA is line-based and case-insensitive; continue a long line with a space and an underscore.
  • A Sub runs code without returning a value; a Function returns a value by assigning it to the function's own name.
  • Declare variables with Dim, optionally with As Type; typed variables are faster and safer than Variants.
  • Variable names must start with a letter and cannot contain spaces, most symbols, or reserved words.
  • Option Explicit forces variable declaration and catches typos that would otherwise create silent Variant bugs.
  • Use & for string concatenation, and Mod / backslash for remainder and integer division.
  • Comments begin with an apostrophe and are ignored at runtime; use them to document intent.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#VBAStudyNotes#VBASyntaxAndVariables#VBA#Syntax#Variables#Procedures#StudyNotes#SkillVeris#ExamPrep