fx Macro Recorder & VBA Basics

Cleaning recorded code — remove .Select

⏱ 13 min

What you'll learn

  • What the recorder writes
  • Why .Select is a problem
  • The cleaning rule

Concept

1. What the recorder writes

Recording "make A1:E1 bold with blue fill, then set column widths" gives something like:

Sub FormatHeader()
'
' FormatHeader Macro
'
    Range("A1:E1").Select
    Selection.Font.Bold = True
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 12611584
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    Selection.Font.Color = RGB(255, 255, 255)
    Columns("A:E").Select
    Selection.ColumnWidth = 15
    Range("A2").Select
End Sub

2. Why .Select is a problem

  • Slow: Excel actually moves the cursor and redraws the screen every time.
  • Fragile: it works on whatever sheet is active. If the user is on the wrong sheet, the wrong data changes.
  • Noisy: half the lines are recorder habits, not your logic.

3. The cleaning rule

When you see:

Something.Select
Selection.DoSomething

merge them:

Something.DoSomething

4. The cleaned version

Sub FormatHeader()
    With Worksheets("Data").Range("A1:E1")
        .Font.Bold = True
        .Font.Color = RGB(255, 255, 255)
        .Interior.Color = RGB(0, 112, 192)
    End With
    Worksheets("Data").Columns("A:E").ColumnWidth = 15
End Sub

Same result, 6 lines instead of 16, and it always works on the Data sheet no matter which sheet is open.

5. What else to delete

Recorded line Keep?
.Pattern = xlSolid, .TintAndShade = 0, .PatternColorIndex = xlAutomatic No — default values
ActiveWindow.ScrollRow = 25 No — you scrolled while recording
Range("A2").Select at the end No — just where you clicked last
Application.CutCopyMode = False Yes, after a copy-paste (clears the marching ants)

6. Copy-paste without the clipboard dance

Recorded:

Range("A1:D20").Select
Selection.Copy
Sheets("Report").Select
Range("A1").Select
ActiveSheet.Paste

Clean:

Worksheets("Data").Range("A1:D20").Copy Destination:=Worksheets("Report").Range("A1")

Values only (fastest, no formats or clipboard):

Worksheets("Report").Range("A1:D20").Value = Worksheets("Data").Range("A1:D20").Value

7. Always name the sheet

Range("A1") alone means "A1 on the active sheet". Write Worksheets("Data").Range("A1") so the macro does the same thing every time. In Lesson 4 we'll store the sheet in a variable to keep lines short.

Common mistakes

Removing .Select but leaving Selection lines behind (error 424 or wrong range). Cleaning code that relies on ActiveCell (relative recording) without replacing it with a real reference. Deleting CutCopyMode = False.

Exercises

mediumRecord a macro that copies A1:C10 from one sheet to another, bolds the header and sets column widths. Then clean it using the rules above and compare the line count. Run it from a third sheet to prove it no longer depends on the active sheet.
Use ThisWorkbook.Worksheets("Data").Range("A1:C10").Copy Destination:=ThisWorkbook.Worksheets("Output").Range("A1"). Inside With ThisWorkbook.Worksheets("Output"), set .Range("A1:C1").Font.Bold = True and .Columns("A:C").ColumnWidth = 18. Remove Select/Activate; running from a third sheet must produce identical Output cells.

Quiz

Clean version of Range("B2").Select + Selection.Value = 5?
Range("B2").Value = 5
Why is ActiveWindow.ScrollRow safe to delete?
It only records scrolling
Fastest way to copy values only?
Dest.Value = Source.Value
Cleaning recorded code — remove .Select · Automation | ExcelWalaa