To set a cell's format in Excel VBA, use the NumberFormat property of a Range object — for example Range("A1").NumberFormat = "0.00" for two decimals, or Range("A1").NumberFormat = "mm/dd/yyyy" for a date. For fonts, fills, borders and alignment, set the corresponding properties (Font.Bold, Interior.Color, Borders, HorizontalAlignment) on the same Range. The steps below show how to apply each one reliably, including how to target whole columns, how to copy formats from one cell to another, and how to avoid the most common errors.
What You'll Need
- Excel on Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > check Developer on Windows; Excel > Preferences > Ribbon & Toolbar on Mac).
- A workbook saved as .xlsm (macro-enabled) if you want to keep the code.
- Basic familiarity with the VBA Editor (Alt+F11 on Windows, Option+F11 on Mac).
- Cells you want to format, and a clear idea of the format code or visual style you want.
Step-by-Step
- Open the VBA Editor. Press Alt+F11 (Windows) or Option+F11 (Mac). In the Project Explorer, right-click your workbook and choose Insert > Module. A blank code window appears. Menu labels can differ slightly by Excel version and platform.
- Write a simple macro that targets one cell. Type:
Sub FormatOneCell()Range("A1").NumberFormat = "0.00"End Sub
This sets A1 to show two decimal places. Run it with F5. If the cell already contains a number, the display updates immediately; the underlying value does not change. - Apply a date or currency format. Replace the format string. For a date:
Range("A1").NumberFormat = "mm/dd/yyyy". For currency:Range("A1").NumberFormat = "$#,##0.00". For a percentage:Range("A1").NumberFormat = "0.0%". Use the exact format code you would type in the Format Cells dialog (Ctrl+1) — VBA accepts the same codes. - Format a whole range at once. Use
Range("A1:C10").NumberFormat = "#,##0"orColumns("B").NumberFormat = "0.00". Setting the property on a multi-cell range applies it to every cell in that range in one call, which is faster than looping. - Change font and fill. Add lines such as
Range("A1").Font.Bold = True,Range("A1").Font.Color = RGB(255, 0, 0),Range("A1").Interior.Color = RGB(220, 235, 255). RGB takes three numbers 0–255 for red, green and blue. To clear a fill, setInterior.ColorIndex = xlNone. - Set alignment and wrapping. Use
Range("A1").HorizontalAlignment = xlCenter,VerticalAlignment = xlCenter, andWrapText = True. The constants xlLeft, xlRight, xlTop and xlBottom also work. - Add borders. For a box around a range:
With Range("A1:C10").Borders.LineStyle = xlContinuous.Weight = xlThin.Color = RGB(0, 0, 0)End With
For a single edge, useRange("A1").Borders(xlEdgeBottom).LineStyle = xlContinuous. The edge constants include xlEdgeLeft, xlEdgeTop, xlEdgeRight, xlEdgeBottom and xlInsideHorizontal. - Copy formatting from an existing cell. Use
Range("B1").CopythenRange("C1").PasteSpecial Paste:=xlPasteFormats, thenApplication.CutCopyMode = False. This copies number format, font, fill, borders and alignment without overwriting the value. - Clear formats when needed.
Range("A1:C10").ClearFormatsremoves all formatting but keeps the data. To reset just the number format, setNumberFormat = "General". - Save and test. Save the workbook as .xlsm. Run the macro on a copy of your data first, because formatting changes are not reversible with Undo inside VBA.
Common Mistakes
- Forgetting the quotes around the format code.
NumberFormat = 0.00is a numeric expression and will error; it must be"0.00". - Assuming NumberFormat changes the value. It only changes display. A cell showing $5.00 still stores 5; calculations use the stored value.
- Using a format code that Excel does not recognise in the current locale. Date and currency codes can differ by region. If a code fails, check the Format Cells dialog on that machine and copy the exact string.
- Formatting the wrong sheet. An unqualified
Range("A1")refers to the active sheet. Qualify it, e.g.Worksheets("Data").Range("A1"), so the macro works regardless of which sheet is visible. - Looping cell by cell for large ranges. Setting the property on the whole range is far faster than a For Each loop over thousands of cells.
- Leaving CutCopyMode on. After PasteSpecial, the marching ants remain and can interfere with later steps; set
Application.CutCopyMode = False.
Tips
- Record a macro while you format a cell through the Format Cells dialog (Developer > Record Macro), then read the generated code to learn the exact property names and constants.
- Store format strings in variables or constants so you can reuse them:
Const MoneyFmt As String = "$#,##0.00". - Use
Withblocks when setting several properties on the same range — it is shorter and easier to read. - Turn off
Application.ScreenUpdating = Falseat the start and back on at the end when formatting many ranges; this speeds up the macro noticeably. - Test on a duplicate sheet. VBA formatting cannot be undone with Ctrl+Z once the macro has run.
Troubleshooting
Runtime error 1004 or “Unable to set the NumberFormat property”. This usually means the sheet is protected, the workbook is shared, or the range reference is invalid. Unprotect the sheet (Review > Unprotect Sheet) or unshare the workbook, then rerun.
The macro runs but nothing changes. Check that you are looking at the right sheet and that the cell is not formatted as Text. A Text-formatted cell ignores number formats; set NumberFormat = "General" first, then reapply.
Dates show as serial numbers. The format string is missing or wrong. Apply a date code such as "yyyy-mm-dd" and confirm the cell contains a real date, not text.
Colors look different than expected. RGB values are absolute; themes and conditional formatting can override them. Clear conditional formatting on the range (Home > Conditional Formatting > Clear Rules) if a rule is winning.
Macro is slow on large ranges. Set the format on the entire range in one statement, disable ScreenUpdating, and avoid selecting or activating cells inside the loop.
If that did not work, step through the macro line by line with F8 to see exactly which statement fails, and check the Immediate Window (Ctrl+G) for the current range address.
Frequently Asked Questions
How do I set a cell to text format in VBA?
Use Range("A1").NumberFormat = "@". The @ symbol is the Text format code. Note that this changes display only; existing numbers are not converted to text automatically.
Can I apply a format to an entire column or row?
Yes. Columns("C").NumberFormat = "0.00" formats all of column C, and Rows(1).Font.Bold = True bolds row 1. For a used range only, use ActiveSheet.UsedRange.NumberFormat = "General".
How do I copy formatting without copying the value?
Use PasteSpecial Paste:=xlPasteFormats after Copy. This transfers number format, font, fill, borders and alignment but leaves the destination's contents intact.
Why does my NumberFormat code work in the dialog but not in VBA?
Usually a quoting or locale issue. In VBA the code must be a string in double quotes, and some regional settings use different separators. Copy the exact string from the Format Cells dialog on the same machine.
Can I format a cell based on its value?
Yes, with an If statement, e.g. If Range("A1").Value < 0 Then Range("A1").Font.Color = RGB(255,0,0). For ongoing rules, conditional formatting is usually better than a one-time macro.
Does formatting a cell change the stored value?
No. NumberFormat, Font, Interior and Borders affect appearance only. Calculations and exports use the underlying value.