Formatting a cell with VBA means setting properties on a Range object, then letting Excel apply them. The shortest working version is Range("A1").Font.Bold = True or Range("A1").Interior.Color = RGB(255, 255, 0), placed inside a Sub in a standard module and run with F5. Everything else — number formats, borders, alignment, column widths, conditional styling — is the same idea with a different property. No add-in is required: VBA ships with the desktop versions of Excel on Windows and Mac, and this guide assumes you are working in one of those, not in Excel for the web.

What You'll Need

  • Excel for Windows or Mac (desktop). The browser version does not run VBA macros.
  • The Developer tab visible. If it is missing, right-click the ribbon and choose Customize the Ribbon, then tick Developer in the right-hand list. On Mac, open Excel > Preferences > Ribbon & Toolbar and enable the Developer tab there.
  • A workbook saved as .xlsm (macro-enabled) if you want the code to survive closing and reopening. A plain .xlsx file loses the macros on save.
  • Macro security set to allow your own code. In the Trust Center, under Macro Settings, the "Disable all macros with notification" option still lets you enable macros for a file you trust.

Step-by-Step

  1. Open the VBA editor. Press Alt+F11 (Windows) or Option+F11 (Mac). The editor opens in its own window, separate from Excel.
  2. Create a module. In the editor, use Insert > Module. A blank code pane appears. Code written here is a general-purpose macro you can run from Excel at any time, which is different from code placed in a worksheet's own code module (that runs on events like a cell change).
  3. Write the Sub wrapper. Type Sub FormatTest() and press Enter. The editor adds End Sub automatically. All your formatting statements go between the two lines.
  4. Target a range explicitly. Never rely on whatever happens to be selected. Write Worksheets("Sheet1").Range("A1:C10"). If Sheet1 is not the real sheet name (the tab label), you get a subscript error, so copy the name exactly as it appears on the tab.
  5. Set the font. For example .Font.Bold = True, .Font.Size = 12, .Font.Name = "Calibri", .Font.Color = RGB(255, 255, 255). Use RGB(red, green, blue) with each value from 0 to 255. For a light grey, RGB(242, 242, 242) works well as a header fill.
  6. Set the fill colour. .Interior.Color = RGB(0, 112, 192) for a blue background. To remove a fill instead, use .Interior.Pattern = xlNone, which is more reliable than setting the colour to white.
  7. Apply a number format. Number formats are strings, and the string must be wrapped in quotes with any internal quotes doubled. Currency: .NumberFormat = "$#,##0.00". Percentage with one decimal: .NumberFormat = "0.0%". Dates: .NumberFormat = "dd/mm/yyyy". Text stays text: prefix a cell value with an apostrophe or set .NumberFormat = "@" before writing to it.
  8. Set alignment and wrapping. .HorizontalAlignment = xlCenter, .VerticalAlignment = xlCenter, .WrapText = True. For headings, .IndentLevel and .Orientation give fine control; orientation is in degrees, so 90 rotates text upward.
  9. Draw borders. Borders need a line style and a weight before they appear. For a box around the range: .Borders.LineStyle = xlContinuous then .Borders.Weight = xlThin. To outline only the outer edge, use .Borders(xlEdgeLeft), xlEdgeTop, xlEdgeRight and xlEdgeBottom individually, or set .Borders(xlInsideHorizontal).LineStyle = xlNone to clear inner lines.
  10. Fix the geometry. Column widths and row heights belong to the whole column or row, not to individual cells, so use Columns("A:C").ColumnWidth = 18 and Rows("1:1").RowHeight = 24. Columns("A:C").AutoFit is often better than guessing a width.
  11. Conditionally highlight rows. Set the base format first, then loop the rows and test a value: For i = 2 To 100: If Cells(i, 3).Value < 0 Then Rows(i).Interior.Color = RGB(255, 199, 206). Close the loop with Next i. Keep the test on the cell that decides the rule, not on the cell you are colouring.
  12. Run it. Press F5 in the editor, or return to Excel and use Developer > Macros, pick the macro name and click Run. If the sheet is protected, formatting fails silently or throws an error — unprotect the sheet first, or unlock the specific cells you need to style.
  13. Save and reuse. Save as .xlsm. To make the formatting apply automatically, paste the body of the Sub into a worksheet code module inside a Private Sub Worksheet_Change(ByVal Target As Range) — but only touch Target, never the whole sheet, and switch off events while you format to avoid an endless loop, or Excel will freeze.

Common Mistakes

Using ActiveCell or Selection. Code that formats the selection does something different every time someone clicks elsewhere. Name the range.

Setting a border weight but no line style. The border never appears. Style first, weight second.

Forgetting the leading dot. Inside a With block, Font.Bold = True without the dot formats the active window's default object instead of the range. Always write .Font.Bold = True.

Trying to set ColumnWidth on a single cell. Range("A1").ColumnWidth = 20 sets width for the whole of column A; if your intent was cell size, that is not something a cell has.

Wrong sheet name. The Name property, not the tab's display text, is what Worksheets() expects — the two match unless the tab has been renamed with special handling. Check the name in the VBA Project pane rather than guessing.

Saving as .xlsx. Excel warns that macros cannot be saved, and if you accept, the code is gone next time you open the file.

Clearing wrong. .Clear removes contents and formats; .ClearFormats removes only formatting; .ClearContents removes only data. Pick deliberately.

Tips

Build formatting once by hand, with the macro recorder switched on, then read the recorded code to see the exact property names Excel used. It is the fastest way to learn the object model.

Define named formats as constants at the top of the module (Const HEADER_FILL = 12611584) so the styling lives in one place. You can get the numeric value of any colour by recording a fill and reading the recorded Color number.

Use With blocks to group statements against one range; it is faster and easier to read. Turn off Application.ScreenUpdating at the start of a long formatting loop and back on at the end — this noticeably speeds up formatting hundreds of rows.

Prefer referencing cells by meaning (Cells(lastRow, 3)) over hard-coded addresses, so the macro keeps working when rows are added. Keep formatting and data-writing in separate Subs to make each one easier to test.

Troubleshooting

Nothing happens when you run it. Check the macro name in Developer > Macros — if it is missing, the workbook is .xlsx or macro security is blocking it. Enable macros for that file, or save as .xlsm.

"Subscript out of range". The sheet name inside Worksheets() does not exist in the active workbook. Confirm it in the Project pane and copy it exactly, including spaces.

"Object required" or "Object doesn't support this property". Usually a missing or extra dot inside a With block, or a property applied to a Range that belongs to another object (such as setting ColumnWidth through a multi-area range).

The format reverts. If a worksheet-event macro re-applies a base style, your manual formatting is being overwritten. Remove the conflicting event code, or format inside that event macro instead.

Excel slows or hangs. An event macro that changes cells is retriggering itself. Add Application.EnableEvents = False at the top and True at the end, including on the error path.

Colours look wrong. RGB values are red-green-blue in that order, and some themes override them. Test on a copy of the sheet before applying a macro to a live workbook.

If all of that fails, copy the macro into a fresh workbook with one clean sheet and run it there. If it works, the issue is in the original sheet — protection, merged cells or existing conditional formatting taking priority.

Frequently Asked Questions

Can I format only part of a cell's text with VBA?

Yes, via the Characters object, for example Range("A1").Characters(1, 5).Font.Bold = True. It only works on text values, not numbers or formulas.

How do I copy formatting from one range to another?

Use SourceRange.Copy then DestinationRange.PasteSpecial Paste:=xlPasteFormats, and clear the clipboard with Application.CutCopyMode = False.

Why does my number format show as text?

Number formats only affect display. If the cell holds text that looks like a number, change the value first — a format cannot convert it.

Does this work in Excel for the web?

No. The browser version does not run VBA. Open the macro-enabled file in the desktop app instead.

Can I format cells when the sheet is protected?

Only if the cells were unlocked before protection. Otherwise unprotect the sheet, format, then reprotect it.

How do I remove all formatting from a range?

Use Range("A1:C10").ClearFormats. This leaves the contents intact.