Specifying Font Color in a Formula

Written by Allen Wyatt (last updated June 19, 2021)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


2

Gregory has a desire to specify font color in a cell based on a formula. For instance, a cell might contain a simple IF function, such as =IF(A1>A2,5,7). If the "5" is returned, he would like the font to be normal, but if the "7" is returned, he would like it red. And, it must be noted, Gregory doesn't want to use a conditional formatting rule.

Short of using a macro (as described shortly) there is no way to specify font color in a formula. The easiest way to do this—well, the easiest way that doesn't use conditional formatting—is to create a custom format for the cell containing the formula. The format itself is quite simple:

[red][=7]General;General

The first part of the custom format (before the semicolon) indicates how positive numbers should be formatted. The portion after the semicolon indicates how negative numbers should be formatted. (The second, negative portion is included because if it is not, then Excel applies the singular format to every value that may appear in the cell, whether it be positive or negative.)

The first portion, for positive values, indicates that if the value in the cell is "7", then the value is shown in red. If it is any other positive value (including "5"), then it is shown in a regular font. If you want additional information about how to create custom formats, you can find a great tutorial at Mynda Treacy's site:

https://www.myonlinetraininghub.com/excel-custom-number-format-guide

The drawback to using custom formats in this way, of course, is that it is actually keyed to the value "7". If you want something a bit more generalized such that if any result where A1 is less than or equal to A2 results in the cell being red, then you might be interested in a macro-based approach. The following example is implemented as a user-defined function:

Function TColor(n1 As Double, n2 As Double) As Boolean
    If n1 <= n2 Then
        Application.Caller.Font.ColorIndex = 3
    Else
        ' Set font to normal
        Application.Caller.Font.ColorIndex = xlAutomatic
    End If
End Function

In order to use the UDF, just add it to your formula, in the following manner:

=IF(A1>A2,5,7)+TColor(A1,A2)

The UDF compares A1 to A2 and, if A1 is larger, then the text color is set to normal. If it is less than or equal to A2, then the text color is set to red.

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 (13874) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365.

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

Opening a Document as Read-Only in Windows Explorer

Want to open your Word document as read-only? Here's a little trick to help you accomplish the task from within Explorer.

Discover More

Inserting the Total Number of Pages in Your Document

Word keeps track of many statistics for each of your documents. One statistic is the total number of pages in the printed ...

Discover More

Understanding Optional Features

Windows 10 includes many features that are not enabled by default. Discover how to turn on or off the optional features ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (ribbon)

Ensuring Conditional Formatting and Data Validation is Copied

If you use an Excel worksheet for entering data (a quite common task, actually), then you need to be concerned with how ...

Discover More

Creating a Center Across Selection Button

The ability to center text across a range of cells has long been a staple of experienced Excel users. Here's how to ...

Discover More

Rounded Corners on Cells

As you are formatting a worksheet, Excel allows you to easily add borders to cells. Adding rounded corners to cells is a ...

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}] (all 7 characters, in the sequence shown) 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 2 + 8?

2021-06-28 10:26:10

J. Woolley

Re. the SetFont and SetFill functions in my previous comment (below), I added an optional Target parameter that permits setting font or fill for any cell or range.


2021-06-23 12:42:55

J. Woolley

This Tip inspired me to add two functions to My Excel Toolbox:
SetFont(Name, Size, Style, Color, Underline, Strikethrough)
and SetFill(Color, PatternStyle, PatternColor)
When these functions appear in a cell formula, they will set that cell's font and/or fill properties. For each function, all property parameters are optional and unchanged if missing.
Using these functions might be more convient than specifying a custom or conditional format for a single cell. They can be used with an IF(...) function to set or reset properties. Each function returns an empty string ("") in a text formula, zero (0) in a numeric formula, or FALSE in a boolean formula.
These functions are in module M_RunMacro. The MyToolbox.xlam add-in file includes all My Excel Toolbox modules and user forms.
See https://sites.google.com/view/MyExcelToolbox/


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu 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.