Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Recording Your First Macro | Introduction to VBA and the Excel Object Model
Excel VBA for Business Automation

Recording Your First Macro

Swipe to show menu

The Macro Recorder is the fastest way to see real VBA code without writing any yourself: you perform an action manually, Excel writes down every click as a VBA statement, and you can replay it — or read it to learn the syntax for the objects you just touched.

Recording a Repetitive Task

  1. On the Developer tab, click Record Macro;
  2. Name it FormatEmployeeHeader (no spaces are allowed in macro names);
  3. Choose This Workbook under "Store macro in."
  4. Click OK — Excel is now recording;
  5. Select row 1 (the header row), make it bold, and apply a light green fill;
  6. Click Stop Recording on the Developer tab.
carousel-imgcarousel-imgcarousel-img

Running the Macro Back

Undo your manual formatting (Ctrl+Z) so the header returns to plain black text. Then click Macros on the Developer tab, select FormatEmployeeHeader, and click Run. Excel reapplies the exact formatting instantly — because it's now replaying the code it wrote while you worked.

Understanding the Generated Code

Open the VBE (Alt+F11) and look inside the new module. You'll see something close to this:

Two things are worth noticing immediately. First, every action you took maps to a line of VBA — selecting a range became Range("A1:F1").Select, and making it bold became Selection.Font.Bold = True.

Second, notice the pattern of Select followed by an action on Selection. That's convenient for the recorder, but it's not how experienced VBA developers write code by hand.

What the Recorder Can't Do

  • It can't make decisions — no If statements, no loops, no conditional logic;
  • It hard-codes exact ranges and values, so it breaks the moment your data changes shape;
  • It can't prompt the user for input or validate anything;
  • It often generates bulkier, slower code than a hand-written equivalent;
  • It cannot talk to other applications like Outlook.

Think of the recorder as a translator, not an automation engine: it's brilliant for learning the names of objects and properties, and useful for quick one-off formatting macros, but real business automation — the kind with logic, loops, and error handling — has to be written by hand. That's the skill this entire course builds toward.

Task

Record, break, and read.

  1. Record a second macro called HighlightIT that selects the Department column and applies a bold, italic font to any cell — don't worry about making it conditional yet, just record the manual formatting.
  2. Run both macros back to back and confirm the formatting reapplies correctly.
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 3

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Section 1. Chapter 3
some-alt