TechBuzzPro

Excel VBA Macro to Format Cells: A Practical Guide

· 6 min read · Apps & Browsers

In this article
  1. What cell formatting means in VBA
  2. Format a specific range with a macro
  3. How to run and save a formatting macro
  4. Choosing a target and avoiding common errors
  5. VBA versus built-in formatting tools
  6. Frequently Asked Questions

An Excel VBA macro can format cells by setting properties such as font, fill color, number format, borders, and alignment. For example, run the macro below to make the selected cells bold with a pale-yellow fill; save a copy of the workbook first, because macro actions cannot be undone with Ctrl+Z.

Sub FormatSelectedCells()
    ' Format the current selection.
    Dim target As Range

    If TypeName(Selection) <> "Range" Then Exit Sub
    Set target = Selection

    With target
        .Font.Bold = True
        .Interior.Color = RGB(255, 255, 153)
        .HorizontalAlignment = xlCenter
        .Borders.LineStyle = xlContinuous
    End With
End Sub

This works in desktop Excel for Microsoft 365 on Windows. The same VBA approach is available in Excel for Mac, though opening the VBA editor and managing macro security differ by platform. Excel for the web can open a macro-enabled workbook, but it cannot create, edit, or run VBA macros.

What cell formatting means in VBA

Excel formatting changes how a value appears, not the underlying value itself. A date stored in a cell remains a date if you change its display format; a currency format does not perform a calculation or convert text into a number. VBA exposes many of the same formatting controls found on Excel’s Home tab through properties of a Range object.

  • Font controls typeface, size, color, bold, and italic settings.
  • Interior controls a cell’s fill color and pattern.
  • NumberFormat controls how numbers, dates, percentages, and currency values display.
  • Borders controls edge lines, styles, and weights.
  • HorizontalAlignment and VerticalAlignment control text placement.
  • WrapText and ColumnWidth can help make displayed content readable.

A macro can apply these properties to one cell, a fixed range, the current selection, or a range chosen by a rule. Choosing the right target is as important as choosing the format: an accidental whole-sheet change can be difficult to spot and tedious to repair.

Format a specific range with a macro

For a repeatable report, a fixed range is often safer than formatting whatever happens to be selected. This example applies a date display, bold heading, and thin bottom border to a heading row, then formats the values beneath it. Adjust the worksheet name and ranges to match the workbook.

Sub FormatSalesReport()
    ' Format a known report layout.
    Dim ws As Worksheet

    Set ws = ThisWorkbook.Worksheets("Report")

    With ws.Range("A1:D1")
        .Font.Bold = True
        .Font.Color = RGB(255, 255, 255)
        .Interior.Color = RGB(31, 78, 121)
        .HorizontalAlignment = xlCenter
        .Borders(xlEdgeBottom).LineStyle = xlContinuous
    End With

    ws.Range("A2:A100").NumberFormat = "mmm d, yyyy"
    ws.Range("B2:D100").NumberFormat = "$#,##0.00"
End Sub

The number-format strings are examples: the dollar symbol is appropriate only when that is the intended display, and the date pattern may display differently under regional settings. Formatting does not change a value’s currency or date meaning. If a report’s row count changes, a hard-coded ending row such as 100 may omit records or style empty cells; use a table or determine the last populated row instead.

How to run and save a formatting macro

  1. Save a backup copy of the workbook. VBA actions generally cannot be reversed with Excel’s Undo command.
  2. Save the working file as an Excel Macro-Enabled Workbook (.xlsm) if it needs to retain VBA code.
  3. On Windows, press Alt+F11 to open the Visual Basic Editor. Choose Insert > Module, then paste the macro into the module.
  4. Return to Excel, select the intended range if the macro uses Selection, and press Alt+F8. Choose the macro and select Run.
  5. Review the result, including number formats and borders, before saving the workbook.

On Mac, open the Visual Basic Editor from the Developer tab or use the available VBA editor shortcut for your keyboard and Excel setup; shortcuts can vary. The Developer tab may need to be enabled in Excel settings. Macro security settings can also prevent a macro from running. Only enable or run macros from sources you trust, since VBA can do more than change formatting.

Choosing a target and avoiding common errors

A selection-based macro is convenient for one-off work but depends on the active selection. If a chart, shape, or other object is selected, code that expects a range may fail; the opening example checks the selection type before proceeding. For important workflows, reference a named worksheet and an explicit range, as in the report example.

Common problems and practical fixes include:

  • “Subscript out of range”: Check that the worksheet name in the code matches the tab name exactly and that the sheet belongs to the workbook referenced by ThisWorkbook.
  • Wrong cells changed: Replace Selection with a qualified range such as ws.Range("A1:D10"), or check the selection before running.
  • Color looks different: Use RGB(red, green, blue) with each component from 0 to 255. For example, RGB(255, 0, 0) is red.
  • Numbers display unexpectedly: Verify the cells contain numeric values rather than text, and review the exact NumberFormat string.
  • Formatting seems to disappear: Conditional formatting can override the appearance set by ordinary cell formatting when its rules apply. Review Home > Conditional Formatting > Manage Rules.

When formatting based on a value, avoid assuming that a fill color is a reliable data field. A macro can inspect a cell’s value and apply a style, but conditional formatting is often easier to maintain when the appearance should update automatically as values change.

VBA versus built-in formatting tools

Use VBA when the same multi-step formatting must be repeated, applied across many sheets, or triggered as part of a larger process. A macro can standardize report layouts and reduce repetitive clicks. It is less suitable when coworkers cannot use macros, when a task is a one-time adjustment, or when workbook security rules restrict VBA.

For manual formatting, select cells and use the options on the Home tab, including Font, Alignment, Number, and Borders. Conditional formatting is a better fit for rules such as highlighting values below zero because Excel updates the appearance as values change. Cell styles can also provide consistent reusable formatting without code. These alternatives are easier to inspect and share, while VBA offers more control over repeatable procedures.

Frequently Asked Questions

Can a VBA macro format cells based on their values?
Yes. VBA can read a cell’s value, test a condition, and apply formatting to that cell or another range. For rules that should update automatically whenever data changes, consider Conditional Formatting instead.

Can I undo a formatting macro with Ctrl+Z?
Usually not. Running VBA clears or bypasses Excel’s normal undo history, so make a backup copy before running a macro that changes a workbook.

Can I use VBA macros in Excel for the web?
No. Excel for the web does not create, edit, or run VBA macros. Use the desktop Excel application to run VBA; the workbook can still be opened in a browser for other supported tasks.

Does changing NumberFormat change the actual cell value?
No. It changes how Excel displays the value. For example, formatting a decimal as a percentage changes its display, not the stored number.

Why does my macro say the worksheet name is invalid?
The worksheet reference must match an existing sheet in the workbook being referenced. Check spelling, spaces, and whether the code should use ThisWorkbook or another workbook reference.