VBA Basics
Write your first VBA code to make Excel Macros smarter, dynamic, and reusable.
✅ What You Will Learn
VBA (Visual Basic for Applications) is the programming language built into Excel. While the Macro Recorder generates VBA code for you, knowing VBA lets you edit that code to handle dynamic data, add logic (IF conditions, loops), create custom functions, and build tools that no Macro Recorder can produce.
You write VBA in the VBA Editor (press Alt+F11 to open it). Code is organised into Modules (where general Macros live) and objects like ThisWorkbook and Sheet1 (where event-driven code lives — e.g. code that runs when a sheet is selected or a cell is changed).
Even basic VBA knowledge — variables, loops, and IF statements — allows you to transform brittle recorded Macros into robust tools that work correctly on any dataset.
Syntax
Examples
📌 Key Points to Remember
- ✓Alt+F11 opens the VBA Editor. Insert → Module creates a new code module.
- ✓Sub starts a procedure, End Sub closes it — all code goes between them
- ✓Dim declares a variable. Use Long for row numbers (not Integer — Long handles over 32,767 rows)
- ✓Cells(row, column) references a cell by row and column numbers. Cells(2,3) = cell C2.
- ✓Always test Macros on a copy of your data first — VBA actions cannot be undone with Ctrl+Z
🏢 Real-World Application
Reconciliation teams at banks write VBA Macros that loop through thousands of transaction rows, match debits with credits using custom logic, flag mismatches, and write results to a summary sheet — all in under a minute. This level of customisation is impossible with formulas alone and would take hours manually. VBA is what turns Excel into a proper data processing tool for finance and operations.
⚠️ Common Mistakes to Avoid
❓ Frequently Asked Questions
Do I need to know programming before learning VBA?
No prior programming experience is needed. VBA syntax is very readable — it resembles plain English. Start with recording Macros, read the generated code, and modify small parts. That is how most analysts learn VBA.
What is the difference between a Sub and a Function in VBA?
A Sub performs actions (it does not return a value). A Function returns a value and can be used as a custom Excel formula (UDF — User Defined Function). =MyFunction(A1) in a cell calls a VBA Function.
How do I debug a VBA Macro that is not working?
Press F8 in the VBA Editor to step through code one line at a time. Hover over variables to see their current values. Add Debug.Print variable to print values to the Immediate Window (Ctrl+G to open it).
✏️ Practice Exercise
Write a VBA Sub that: (1) Finds the last row of data in column A dynamically, (2) Loops through each row from row 2 to lastRow, (3) If the value in column B is above 50000, writes "Target Met" in column C; otherwise writes "Below Target", (4) After the loop, shows a MsgBox displaying how many rows were processed. Test it on a dataset of at least 20 rows.
Learn Excel with Live Trainer Guidance
These tutorials give you the foundations. Our live Excel course at EVIKA Academy, Noida teaches you to build real dashboards on actual business data — with a trainer who uses Excel professionally every day.