Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA Range Format Cells: Step-by-Step Guide

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To format cells in Excel VBA, you use the Range object's NumberFormat, Font, Interior, Borders, HorizontalAlignment and related properties. For example, Range("A1:B10").NumberFormat = "0.00" applies two decimal places, and Range("A1").Font.Bold = True makes A1 bold. The exact properties you set depend on what you want to change, but the pattern is always: identify the range, then set one or more format properties.

What You'll Need

  • Microsoft Excel (any recent version) with the Developer tab enabled. If you don't see the Developer tab, right-click the ribbon and choose Customize the Ribbon, then tick Developer in the right-hand list.
  • A workbook saved as .xlsm (macro-enabled) if you plan to keep your VBA code.
  • Basic familiarity with the VBA editor: press Alt+F11 to open it, and use Insert > Module to add a code module.

Step-by-Step

  1. Open the VBA editor. Press Alt+F11 (on Mac, Fn+Alt+F11 or the Developer tab > Visual Basic). The editor opens in a separate window.
  2. Insert a module. In the editor, choose Insert > Module. A blank code window appears. All the code below goes there.
  3. Write a simple formatting macro. Start with a sub procedure. For example:
    Sub FormatRange()
        Range("A1:C5").NumberFormat = "0.00"
    End Sub
    This sets two decimal places on A1:C5. Run it with F5 or by placing the cursor inside the sub and pressing F5.
  4. Format numbers with NumberFormat. Common codes: "0" for integers, "0.00" for two decimals, "#,##0" for thousands separator, "0%" for percentage, "mm/dd/yyyy" for dates, and "@" for text. Assign the code as a string to the range's NumberFormat property.
  5. Change font appearance. Use the Font object: With Range("A1:C5").Font then set .Bold = True, .Italic = True, .Size = 12, .Name = "Calibri", .Color = RGB(255, 0, 0). Close with End With. RGB takes red, green, blue values from 0 to 255.
  6. Set cell background colour. Use the Interior object: Range("A1:C5").Interior.Color = RGB(220, 230, 241). To remove fill, set .Interior.ColorIndex = xlNone.
  7. Add borders. Use the Borders collection: With Range("A1:C5").Borders then .LineStyle = xlContinuous, .Weight = xlThin, .Color = RGB(0, 0, 0). To target specific edges, use Borders(xlEdgeBottom) or Borders(xlInsideHorizontal).
  8. Align text. Set Range("A1:C5").HorizontalAlignment = xlCenter and .VerticalAlignment = xlCenter. For wrapping, set .WrapText = True. For merged-looking headers, use .Merge only if you really need merged cells; otherwise avoid merging because it complicates later data handling.
  9. Format an entire column or row. Use Columns("A").NumberFormat = "0.00" or Rows(1).Font.Bold = True. To format the used range only, use ActiveSheet.UsedRange.
  10. Apply multiple formats in one block. Combine properties inside a With block for readability:
    With Range("A1:C5")
        .NumberFormat = "#,##0.00"
        .Font.Bold = True
        .Interior.Color = RGB(255, 255, 204)
        .HorizontalAlignment = xlCenter
    End With
  11. Test and save. Run the macro, check the result on the sheet, then save the workbook as .xlsm. If you close without saving, the code is lost.

Common Mistakes

  • Forgetting quotes around format codes. NumberFormat = 0.00 is invalid; it must be NumberFormat = "0.00".
  • Using Value instead of NumberFormat. Setting .Value = "0.00" writes text, not a number format.
  • Assuming formatting changes the underlying value. NumberFormat only changes display; the stored value stays the same. To round actual values, use WorksheetFunction.Round or similar.
  • Not qualifying the sheet. Range("A1") refers to the active sheet. If another sheet is active when the macro runs, you format the wrong sheet. Use Worksheets("Sheet1").Range("A1") to be explicit.
  • Merging cells unnecessarily. Merged cells break sorting, filtering and many VBA loops. Prefer Center Across Selection for headers.
  • Leaving the macro recorder's output unedited. Recorded code often includes Select and Selection lines that are slow and fragile. Replace them with direct range references.

Tips

  • Use With blocks to avoid repeating the range reference and to make code easier to read.
  • Store format codes in constants or variables if you reuse them, e.g. Const MoneyFormat As String = "#,##0.00".
  • To copy formatting from one range to another, use Range("A1").Copy then Range("B1").PasteSpecial Paste:=xlPasteFormats, then clear the clipboard with Application.CutCopyMode = False.
  • Turn off screen updating while a macro runs many format changes: Application.ScreenUpdating = False at the start, then set it back to True at the end. This speeds up large operations.
  • Use ClearFormats to reset a range before applying new formatting: Range("A1:C5").ClearFormats.
  • Test on a copy of your workbook. Formatting macros are easy to undo with Ctrl+Z only if the macro has not cleared the undo stack; running a macro typically clears it, so keep a backup.

Troubleshooting

  • Macro does nothing. Check that the code is in a standard module (not a sheet module) and that you pressed F5 with the cursor inside the correct sub. Also confirm the workbook is macro-enabled (.xlsm) and macros are enabled in the Trust Center.
  • Run-time error 1004. This often means the range reference is invalid or the sheet is protected. Unprotect the sheet or adjust the range. Also check that you are not trying to format a chart or a protected cell.
  • Formatting appears on the wrong sheet. Qualify the range with a worksheet object, as noted in Common Mistakes.
  • Number format shows as text. If the cell already contains text, NumberFormat will not convert it. Clear the cell and re-enter the value, or use Range("A1").Value = Val(Range("A1").Value) to convert.
  • Colours look different than expected. RGB values are 0–255 per channel. If you use a theme colour, the displayed shade can vary with the workbook theme. For consistent results, use explicit RGB values.
  • Borders do not appear. Set LineStyle before Weight and Color; some border settings are ignored if LineStyle is xlNone. Also ensure you are targeting the correct border edge.

Frequently Asked Questions

How do I format a whole column with VBA?

Use Columns("A").NumberFormat = "0.00" or Range("A:A").Font.Bold = True. This affects the entire column, including empty cells.

Can I apply conditional formatting with VBA?

Yes, use Range("A1:A10").FormatConditions.Add or AddColorScale etc. The syntax mirrors the conditional formatting rules in the Excel interface.

What is the difference between NumberFormat and NumberFormatLocal?

NumberFormat uses the US English format codes regardless of your system locale. NumberFormatLocal uses your local date and decimal separators. For macros that will run on different regional settings, NumberFormat is usually safer.

How do I copy formatting from one cell to another with VBA?

Use Range("A1").Copy then Range("B1").PasteSpecial Paste:=xlPasteFormats, then Application.CutCopyMode = False. Alternatively, set each property individually.

Why does my macro run slowly on large ranges?

Formatting cell by cell is slow. Apply formatting to the whole range at once, and disable screen updating with Application.ScreenUpdating = False during the operation.

Can I undo a VBA formatting macro?

Running a macro clears Excel's undo history, so Ctrl+Z will not undo it. Keep a backup or write a macro that restores the previous formatting if you need to reverse changes.