Macros and Basic VBA

Harry · 12 Sep 2026 · 11 views

What Macros Are

A macro records your clicks and keystrokes as VBA code, then replays them. Use them for repetitive tasks: cleaning reports, standard formatting, renaming sheets.

Enable Developer Tab

File → Options → Customize Ribbon → check Developer.

Record Your First Macro

  1. Developer → Record Macro; name it (no spaces).
  2. Do the steps: select a range, format, add borders, etc.
  3. Stop Recording; press Alt+F8, select the macro, Run.

Assign it to a button: Insert → Shapes or Developer → Insert → Button Control.

The Generated Code

Sub FormatReport()
    Range("A1:C10").Select
    Selection.Font.Bold = True
    With Selection.Interior
        .Color = RGB(217, 217, 217)
    End With
End Sub

Open the editor with Alt+F11. Recorded code tends to Select too much - remove the .Select lines by setting properties directly:

Range("A1:C10").Font.Bold = True

Macro Security

Workbook macros need the .xlsm extension. Set File → Options → Trust Center to Disable all macros with notification, and only enable files you trust - macros can run anything on your machine.

Key Points

  • Record first, then tidy the code - recording is the fast way to learn VBA.
  • Macros that reference the exact range are brittle; prefer the active range or named ranges.
  • Store trusted workbooks in .xlsm and keep macro security at Notification.
Share this post:

Comments (0)

Please login or register to comment.