Excel VBA Range Format Cells: Step-by-Step Guide
In this article
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
- 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.
- Insert a module. In the editor, choose Insert > Module. A blank code window appears. All the code below goes there.
- Write a simple formatting macro. Start with a sub procedure. For example:
This sets two decimal places on A1:C5. Run it with F5 or by placing the cursor inside the sub and pressing F5.Sub FormatRange() Range("A1:C5").NumberFormat = "0.00" End Sub - 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. - Change font appearance. Use the Font object:
With Range("A1:C5").Fontthen set.Bold = True,.Italic = True,.Size = 12,.Name = "Calibri",.Color = RGB(255, 0, 0). Close withEnd With. RGB takes red, green, blue values from 0 to 255. - Set cell background colour. Use the Interior object:
Range("A1:C5").Interior.Color = RGB(220, 230, 241). To remove fill, set.Interior.ColorIndex = xlNone. - Add borders. Use the Borders collection:
With Range("A1:C5").Bordersthen.LineStyle = xlContinuous,.Weight = xlThin,.Color = RGB(0, 0, 0). To target specific edges, useBorders(xlEdgeBottom)orBorders(xlInsideHorizontal). - Align text. Set
Range("A1:C5").HorizontalAlignment = xlCenterand.VerticalAlignment = xlCenter. For wrapping, set.WrapText = True. For merged-looking headers, use.Mergeonly if you really need merged cells; otherwise avoid merging because it complicates later data handling. - Format an entire column or row. Use
Columns("A").NumberFormat = "0.00"orRows(1).Font.Bold = True. To format the used range only, useActiveSheet.UsedRange. - Apply multiple formats in one block. Combine properties inside a
Withblock for readability:With Range("A1:C5") .NumberFormat = "#,##0.00" .Font.Bold = True .Interior.Color = RGB(255, 255, 204) .HorizontalAlignment = xlCenter End With - 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.00is invalid; it must beNumberFormat = "0.00". - Using
Valueinstead ofNumberFormat. 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.Roundor 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. UseWorksheets("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
SelectandSelectionlines that are slow and fragile. Replace them with direct range references.
Tips
- Use
Withblocks 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").CopythenRange("B1").PasteSpecial Paste:=xlPasteFormats, then clear the clipboard withApplication.CutCopyMode = False. - Turn off screen updating while a macro runs many format changes:
Application.ScreenUpdating = Falseat the start, then set it back toTrueat the end. This speeds up large operations. - Use
ClearFormatsto 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
LineStylebeforeWeightandColor; some border settings are ignored if LineStyle isxlNone. 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.