Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Changing Font Face and Size Conditionally.

Changing Font Face and Size Conditionally

by Allen Wyatt
(last updated June 21, 2018)

9

Robin asks if there is a way to use Excel's conditional formatting capabilities to change the font used in a cell or to change the font size in a cell. The short answer is no, that can't be done—at least not with conditional formatting. (The controls that allow you to specify font name and size are grayed-out in the formatting dialog box used with conditional formatting.)

You can, however, use a macro to examine cell contents and make changes in the appearance of a cell. Consider the following macro, which examines any cells you have selected when you run the macro. If any of the cells have a length of more than two characters or a value of more than 10, then the cell's font is changed.

Sub DoReformat()
    Dim rCell As Range

    For Each rCell In Selection.Cells
        If Len(rCell.Text) > 2 Or _
          Val(rCell.Value) > 10 Then
            rCell.Font.Name = "Arial"
            rCell.Font.Size = 16
        Else
            rCell.Font.Name = "Times New Roman"
            rCell.Font.Size = 12
        End If
    Next
End Sub

To use the macro, just select the cells you want changed and then run the macro. If you want the formatting to change more automatically, then you can have the macro check to see if a change was made within a certain range of cells:

Private Sub Worksheet_Calculate()
    Dim rng As Range
    Dim rCell As Range

    Set rng = Range("A1:A10")

    For Each rCell In rng
        If Len(rCell.Text) > 2 Or _
          Val(rCell.Value) > 10 Then
            rCell.Font.Name = "Arial"
            rCell.Font.Size = 16
        Else
            rCell.Font.Name = "Times New Roman"
            rCell.Font.Size = 12
        End If
    Next
End Sub

This macro, when added to the worksheet object, will run every time the worksheet is recalculated. It checks the range A1:A10, applying the same tests as in the previous macro. The result is that the formatting of the cells is checked and changed continuously. To have the macro check a different range, just change the addresses assigned to the rng variable near the beginning of the macro.

One drawback of this macro is that it can get sluggish if you have a very large range for it to check. It will go very quickly if you are checking A1:A10 (ten cells), but may go much slower if you are continually checking B2:N465 (over 6,000 cells). In that case, you may want to design the macro so it runs whenever the worksheet is changed, but only takes action if the change was done to a cell in your target range. The following version is also added to the worksheet object:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rCell As Range

    If Union(Target, Range("A1:A10")).Address = _
      Range("A1:A10").Address Then
        Application.EnableEvents = False
        For Each rCell In Target
            If Len(rCell.Text) > 2 Or _
              Val(rCell.Value) > 10 Then
                rCell.Font.Name = "Arial"
                rCell.Font.Size = 16
            Else
                rCell.Font.Name = "Times New Roman"
                rCell.Font.Size = 12
            End If
        Next
        Application.EnableEvents = True
    End If
End Sub

The macro uses the Union function to check whether the cells changed (passed to the event handler in the Target variable) have any overlap with the range you want checked. If they do, then the checking is done on the cells in the Target range.

One thing to keep in mind with macros that affect formatting is that if you have conditional formatting applied to a cell that is also checked by a macro, the formatting in the conditional formatting takes precedence over the formatting in the macro. If your macro is changing font name and font size, this isn't a big concern because conditional formatting won't affect these attributes. However, if you change your macro to also change a different format attribute—such as cell color—and that attribute is also changed by the conditional format, then it won't look like the macro did anything because Excel uses the conditional formatting in preference to what the macro does.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2380) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Changing Font Face and Size Conditionally.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Canceling an Edit

When editing a cell, you may want to cancel the edit at some point. There are two ways to do this, both described in this ...

Discover More

Columns in a Text Box

Want to divide a text box into columns? Word doesn't allow you to do this, but there are ways to work around the limitation.

Discover More

Selecting Individual Cells in a Table

Many times before applying formatting or doing another operation, you need to select an individual cell in a table. ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (menu)

Exporting Latitude and Longitude

A handy way to store latitude and longitude values in Excel is to treat them as regular time values. When it comes around ...

Discover More

Creating Two-Line Custom Formats

Creating custom formats is a very powerful way to display information exactly as you want it to appear. Most custom ...

Discover More

Understanding Underlines

Excel provides a variety of underlining styles you can use when you need to underline information within a cell. Here's ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is three more than 1?

2018-03-06 10:19:56

Mark

Great information here. Question, if you want to put multiple "If" statements in this macro, how would you structure the formula?

For example, if you wanted the Length > 2 to yield Wingdings and the Value > 10 to yield Arial how would that change the formula?

I've tried different variations but they are showing errors.


2017-05-01 17:18:07

john johnson

That is AWESOME!!!
How would I see if a cell is being used, if it is, and a button (click here) is pressed, take the information in that cell, change the font size AND Style, and copy the results in a different cell..?


2017-01-05 14:54:05

Kevin

I want to use a macro to change the font size of numbers within a cell. See example:

他1上4学2总3是4迟2到4

This is Chinese text and the numbers indicate pronunciation tone. If the cell is formatted to size 16 then the numbers are too large. I would like to reduce the number size to 10 and leave the text at 16.

Thanks for your help. Kevin


2014-10-17 09:31:51

Barry

@Alex

What you ask for is quite easily done.

You would need to be a bit more specific before any code could be written. In Jeff's example, he specified the controlling range is column A and the controlled range is the entire row containing the changed controlling cell, and the change criteria is the word "Large".


2014-10-17 09:20:33

Barry

@Jeff

The following macro will do what you want it needs to be placed on the codepage of the worksheet that you want it to operate on.

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Range("A1").Column <> 1 Then Exit Sub
With Target.Range("A1")
If LCase(.Value) = "large" Then
.EntireRow.Font.Name = "Arial"
.EntireRow.Font.Size = 16
Else
.EntireRow.Font.Name = "Times New Roman"
.EntireRow.Font.Size = 12
End If
End With
End Sub


2014-10-16 17:07:50

Jeff

Is there a way to apply a change to an entire row? And can it be applied based on whether a cell contains a string of text? For example, in range A4:L10, I want to enlarge the font size for each row in which the cell in column A contains the word "Large".


2014-10-06 12:33:15

Alex

This code was very helpful. One further question though: what about VBA code that will change the font of a range of cells based on the values of a different range? Is that possible?

Thanks!


2013-10-17 21:52:43

Dicu-Sava Cristian

First macro (Sub DoReformat), work only with text.
What about working with text and numbers ?
I tryed to change Text with Characters but ... got an error. :)







2013-05-16 11:11:11

Mad

Thanks for that tips!
I'm able in Excel, but not very good at programming in VBA. But this will help me alot in my personal files. They will look better.

Thanks again :)


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.