Smart Systems for Home

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

Excel VBA Range Font: Formatting Cells with Code

· 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 change the font of a range in Excel VBA, you use the Range.Font property, which lets you set the name, size, bold, italic, color, and other font attributes. The shortest working example is Range("A1").Font.Bold = True. This guide walks you through the essential syntax, common formatting tasks, and how to avoid typical pitfalls.

What You'll Need

  • Microsoft Excel (2010 or later is fine; the VBA editor works the same in all recent versions).
  • Basic familiarity with opening the VBA editor (press Alt + F11) and inserting a module.
  • A workbook where you can test the code (save a copy first if it contains important data).

Step-by-Step

  1. Open the VBA editor. In Excel, press Alt + F11. In the editor, go to Insert > Module to create a new code module.
  2. Write a subroutine. Type Sub FormatFont() and press Enter. The editor will add End Sub automatically. All your code goes between these two lines.
  3. Reference the range you want to format. Use Range("A1:C10") for a specific range, or Range("A1") for a single cell. You can also use Cells(1,1) for A1, or Selection if you want the user to select a range first.
  4. Set the font name. Type Range("A1").Font.Name = "Calibri" to change the font family. Use the exact name as it appears in the font dropdown (e.g., "Arial", "Times New Roman").
  5. Set the font size. Use Range("A1").Font.Size = 12 to set the point size. You can use any integer between 1 and 409.
  6. Apply bold, italic, or underline. These are Boolean properties: Range("A1").Font.Bold = True, Range("A1").Font.Italic = True, and Range("A1").Font.Underline = True. Set them to False to remove the effect.
  7. Change the font color. Use Range("A1").Font.Color = RGB(255, 0, 0) for red. The RGB function takes three numbers (0–255) for red, green, and blue. You can also use vbRed, vbBlue, etc., or a hex value like &HFF0000.
  8. Format multiple properties at once. Use a With block to avoid repeating the range reference:
    With Range("A1:C10")
        .Font.Name = "Arial"
        .Font.Size = 14
        .Font.Bold = True
        .Font.Color = RGB(0, 100, 200)
    End With
  9. Run the code. Press F5 while the cursor is inside the subroutine, or go to Run > Run Sub/UserForm. Check the formatted range in your worksheet.

Common Mistakes

  • Using the wrong range reference. A common error is typing Range(A1) without quotes. Always use Range("A1") or Cells(1,1).
  • Forgetting to set the range object. If you write Font.Bold = True without a range, VBA will throw an error. Always prefix with the range.
  • Using Font.ColorIndex incorrectly. ColorIndex uses a different numbering system (1–56). If you want RGB colors, use Font.Color instead.
  • Assuming the font name is case-sensitive. It isn't, but it must match the exact name (e.g., "Arial" not "arial"). Use the name as it appears in the font list.
  • Applying formatting to a large range that includes empty cells. This is fine, but it can slow down the macro. Limit the range to the used area if possible.

Tips

  • Use With blocks to make your code cleaner and faster, especially when setting multiple properties.
  • Use Range("A1").CurrentRegion to select the contiguous block of data around A1, which is handy for formatting entire tables.
  • Test on a single cell first before applying to a large range, to avoid unexpected results.
  • Use Font.Strikethrough = True for strikethrough, and Font.Superscript = True for superscript, if needed.
  • Combine with With and For Each loops to format multiple non-contiguous ranges, but be careful with performance.

Troubleshooting

If the font does not change:

  • Check that the code is actually running. Add a breakpoint (click the left margin next to a line) and press F8 to step through.
  • Make sure you are referencing the correct worksheet. If your code is in a module, it affects the active sheet. Use Worksheets("Sheet1").Range("A1") to specify a sheet.
  • Ensure the range is not on a protected sheet. If the sheet is protected, VBA cannot change formatting unless the sheet is unprotected first.

If you get a "Method 'Font' of object 'Range' failed" error:

  • This usually means the range reference is invalid. Check for typos in the range address, and ensure the worksheet exists.

If the color appears wrong:

  • Remember that RGB values go from 0 to 255. If you use a value above 255, VBA will throw an error. Also, the color may look different on screen vs. print; test with a simple color like vbRed.

Frequently Asked Questions

Can I change the font of an entire column or row?

Yes. Use Range("A:A").Font.Bold = True for the entire column A, or Range("1:1") for the entire row 1. Be careful, as this affects all cells in that column/row, including empty ones.

How do I set the font to the default (e.g., Calibri 11)?

Set Range("A1").Font.Name = "Calibri" and Range("A1").Font.Size = 11. You can also use Application.StandardFont to get the default font name, but that is a system setting, not per-workbook.

Can I use a variable for the range?

Yes. For example: Dim rng As Range then Set rng = Range("A1:B5") and then rng.Font.Italic = True.

How do I clear all font formatting from a range?

Use Range("A1").Font.ResetFont() (available in Excel 2010 and later). This resets the font to the default for that cell style.

Why does my code work in one workbook but not another?

Likely because the other workbook has a different sheet name, or the range is on a protected sheet. Also, check if the other workbook is in a different Excel version that might handle certain properties differently (rare, but possible).

Can I format only the first letter of a cell?

Yes, but you need to use the Characters property: Range("A1").Characters(1,1).Font.Bold = True makes the first character bold. This works for individual characters within a cell.