Record a macro in Excel and clean up the code

The macro recorder watches what you click and type in Excel and turns it into VBA code. It is the quickest way to a first macro, and handy when you do not know what a command is called. The code it writes is clumsy, though. Here you learn to read it and cut it down to what matters.

In short: Developer > Record Macro, give it a name, OK, do the steps in Excel, Stop Recording. When you clean up the recorded code, Range("B5").Select plus Selection.Font.Bold = True becomes one line: Range("B5").Font.Bold = True.

What you need to know: VBA basics, meaning how a macro is built with Sub and End Sub and where it lives in the VBA editor.

Try it right now

Below is code the way the recorder writes it: it selects B5, types a sum formula, selects B5 again and makes the selection bold. Run it once as it is, then clean it up. Check expects code without .Select.

Task Excel recorded this macro: it writes a sum formula into B5 and makes B5 bold. Clean it up: same result, but without .Select and Selection. B5 should still hold a formula, not a typed-in number.

ABCD
1MonthSales
2January18,400
3February16,950
4March21,300
5Total

How to record a macro

  1. Start recording: Developer tab, Code group, Record Macro. If the tab is missing, switch it on under File > Options > Customize Ribbon, as shown in VBA basics.
  2. Name it: In the Macro name box. The first character must be a letter; after that, letters, numbers and underscores are allowed, spaces are not. Format_monthly_report works, Format monthly report does not.
  3. Shortcut key (optional): A letter to start the macro later. Careful: while the workbook is open, it overrides any built-in Excel shortcut on the same key.
  4. Store macro in: Usually This Workbook. If you want the macro in every Excel file, pick Personal Macro Workbook; Excel then creates a hidden file called Personal.xlsb.
  5. Record: Click OK and do everything exactly as the macro should do it later.
  6. Stop: Developer > Stop Recording. The code is now in a module in the VBA editor (Alt+F11).

What recorded code looks like

Say you click B5, type a sum, press Enter, click B5 again and then click Bold. The module will contain something like this:

Sub Macro1()
'
' Macro1 Macro
'

'
    Range("B5").Select
    ActiveCell.FormulaR1C1 = "=SUM(R[-3]C:R[-1]C)"
    Range("B6").Select
    Range("B5").Select
    Selection.Font.Bold = True
End Sub
LineMeaning
' Macro1 MacroA comment. VBA ignores everything after an apostrophe. The description from the dialog ends up here too.
Range("B5").SelectYour click on B5. A step of its own for Excel, usually pointless for the result.
ActiveCell.FormulaR1C1 = …What you typed into the active cell. The recorder writes formulas in R1C1 style: R[-3]C means three rows up, same column.
Range("B6").SelectThe Enter key: the selection moves one cell down.
Selection.Font.Bold = TrueBold for whatever is selected right now.

After the clean-up, two lines are left:

Sub TotalBold()
    Range("B5").Formula = "=SUM(B2:B4)"
    Range("B5").Font.Bold = True
End Sub

The rule: every pair of "select a cell" and "do something with the selection" turns into one line that talks to the cell directly. Clicks that change nothing, like the jump to B6, go. The result is shorter, faster and still works when a completely different cell is selected at the start.

Why .Select gets in the way

A macro full of Select works like a person with a mouse: point first, then act. That costs time, because Excel shows every selection on screen, and it makes the code fragile. If a line says Selection.Font.Bold = True and the matching Select is missing, it simply bolds whatever the user happened to select. Code without Select does not care where the selection is.

In VBA you write formulas with .Formula in A1 style or with .FormulaR1C1 as recorded. Either way, function names are in English and arguments are separated by commas, whatever language your Excel uses.

One more exercise: fill down without AutoFill

While recording, you drag a formula down with the fill handle, and the recorder turns that into Selection.AutoFill. In code there is a shorter way: write the formula into the whole range at once, and Excel adjusts the references for each row.

Task What was recorded: a formula quantity times price in D2, filled down to D6, and D2:D6 formatted as a number with two decimals. Clean up the macro without .Select and Selection. D2:D6 should hold formulas.

ABCD
1ItemQtyPriceAmount
2Copy paper A4204.85
3Black toner362.90
4Lever arch file252.39
5Sticky notes121.75
6Ballpoint pens500.89

Absolute or relative recording

By default Excel records fixed addresses: Range("B5") is always B5, wherever you start. The Developer tab has a Use Relative References button. If it is on while you record, the recorder writes moves relative to the active cell, such as ActiveCell.Offset(1, 0) for "one row down". Those macros act wherever the selection is when they start. For most jobs it is better to address the cells directly in code. The next lesson shows how to do that with numbers instead of addresses.

What the recorder cannot do

  • Make decisions: "If the amount is over 1,000, then …" cannot be recorded. That is what If Then Else is for.
  • Repeat: Edit five rows and the recorder writes down exactly five rows. A list of 500 rows needs a loop.
  • Remember things: Holding on to a value and using it later takes variables.
  • Read values from cells: What you type is fixed in the code. Type "Sales March" and it still says March in April.

The recorder is still worth using: if you do not know what a command is called in VBA, record it quickly and look. Then write a clean version of that line into your own macro.

Common mistakes

Deleting Select but keeping Selection

If you delete only Range("B5").Select, Selection.Font.Bold = True is left without a target, and the macro formats whatever cell is selected. In the exercise, Check then says "Bold is not right in B5 (Font.Bold)." Replace Selection with the cell itself.

Result instead of formula

Range("B5").Value = 56650 looks the same on the sheet, but it is a fixed number. If a sales figure changes, the total is wrong. The exercise reports: "B5 should contain a formula (Range("B5").Formula = "=…"), not just its result."

Formulas from a non-English Excel

If you work with a German or French Excel, the recorder still writes English function names, and so must you in .Formula. .FormulaLocal takes the names of your Excel language, but the macro then only works in Excel set to that language.

FAQ

Where do I find the recorded macro?

In the VBA editor (Alt+F11), in the Project Explorer on the left under Modules. Excel adds a module the first time you record; double-click it to open the code.

Does the recorder capture mouse movements?

No, only actions: selecting cells, typing, ribbon commands, formatting. How long you think in between does not matter.

Can I undo what a recorded macro did?

No. Changes made by a macro cannot be undone with Ctrl+Z. Try new macros on a copy of the file.

Is recording worth it if I rewrite everything afterwards?

Yes, as a reference. For borders, number formats or sorting, the recorder shows you right away which property you need. The rest is faster by hand.

Learn this next: Range and Cells

Point to cells and ranges directly, by address or by row and column number. The basis for everything that comes later with loops.

Practice Range and Cells

Write to us

Choose language

Select your preferred language