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.
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 FunctionDeclaring 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
1. What is the difference between a Sub and a Function in VBA?
2. Which keyword declares a variable in VBA?
3. What does Option Explicit do?
4. Which operator should you use to join two strings in VBA?
5. What does the expression 17 Mod 5 evaluate to?
Was this page helpful?
You May Also Like
VBA Data Types and Scope
How VBA stores values and controls their visibility: the core data types, variable scope and lifetime, constants, and object variables.
Your First VBA Macro
Write, run, and debug your very first macro: set up the Developer tab, add a module, greet the user with MsgBox, write to a cell, and step through the code.
What Is VBA?
An introduction to Visual Basic for Applications: what it is, where it runs, and why it remains the workhorse of desktop Office automation.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics