Coloring Cells with Formulas

Written by Allen Wyatt (last updated November 7, 2020)
This tip applies to Excel 97, 2000, 2002, and 2003


1

Cells in a worksheet can contain values or they can contain formulas. At some time, you may wish to somehow highlight all the cells in your worksheet that contain formulas by coloring those cells. There are several ways you can approach and solve this problem. If you don't have a need to do the highlighting that often, a manual approach may be best. Follow these steps:

  1. Press either F5 or Ctrl+G. Excel displays the Go To dialog box.
  2. Click Special. Excel displays the Go To Special dialog box. (See Figure 1.)
  3. Figure 1. The Go To Special dialog box.

  4. Select the Formulas radio button.
  5. Click OK.

At this point, every cell in the worksheet that contains formulas is selected, and you can add color to those cells or format them as desired. This approach can be automated, if desired, by using a macro like the following:

Sub ColorFormulas()
    ActiveSheet.Cells.SpecialCells(xlCellTypeFormulas, 23).Select
    With Selection.Interior
        .ColorIndex = 6
        .Pattern = xlSolid
    End With
End Sub

You can run this macro as often as necessary in order to highlight the various cells that contain formulas. The only problem is that if a formula is deleted from a cell that was previously highlighted, the highlighting remains; it is not removed automatically. In this case, a different macro approach is mandated. This macro acts on a range of cells you select before running the macro.

Sub ColorFunction()
    For Each cell In Selection
        If cell.HasFormula Then
            With cell.Interior
                .ColorIndex = 6
                .Pattern = xlSolid
            End With
        Else
            cell.Interior.ColorIndex = xlNone
        End If
    Next cell
End Sub

The macro checks each cell in the range. If the cell contains a formula, then it is highlighted. If the cell does not contain a formula, then the highlight is turned off.

Another potential solution is to use a user-defined function along with the conditional formatting capabilities of Excel. Create the following function in the VBA Editor:

Function CellHasFormula(c As Range) As Boolean
    CellHasFormula = c.HasFormula
End Function

With this function in place, you can use the conditional formatting capabilities of Excel (detailed elsewhere in ExcelTips) to check what the formula returns. In other words, you would set a conditional format that checked the result of this formula:

=CellHasFormula(A1)

If the result is true (the cell contains a formula), then your conditional format is applied.

It is interesting to note that you don't have to create a VBA macro to use the conditional formatting route, if you don't want to. (Some people have a natural aversion to using macros.) Instead, you can follow these steps:

  1. Press Ctrl+F3. Excel displays the Define Name dialog box.
  2. In the Names field (at the top of the dialog box), enter a name such as FormulaInCell.
  3. In the Refers To field (at the bottom of the dialog box), enter the following:
  4. =GET.CELL(48,INDIRECT("rc",FALSE))
    
  5. Click OK.

Now you can follow the techniques previously outlined for setting up the conditional formatting. The only difference is that the conditional format should check for the following formula, instead:

=FormulaInCell

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 (2766) applies to Microsoft Excel 97, 2000, 2002, and 2003.

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

Splitting a Table

Table getting too long? Need to move part of a table to somewhere else in your document? You can easily split an existing ...

Discover More

Creating a Numbered List

Numbered lists provide a 1-2-3 way of organizing your document. You can create numbered lists very easily using the ...

Discover More

Disappearing Column Formatting

Two things go into making your documents look just right: content and formatting. If the formatting seems to disappear on ...

Discover More

Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!

More ExcelTips (menu)

Shrinking Cell Contents

Need to cram a bunch of text all on a single line in a cell? You can do it with one of the lesser-known settings in Excel.

Discover More

Decimal Tab Alignment

If you are familiar with decimal tabs in Word, you may wonder if you can set the same sort of alignment in Excel. The ...

Discover More

Changing Cell Patterns

You can shade your cells by filling them with a pattern. Here's how to select the pattern you want used.

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 two more than 7?

2022-02-11 15:36:52

HarryS

Private Sub ShowFormula_Click()
Dim RaLook As Range: Set RaLook = Range([e2]) ' cell E2 has range like D9:J14
Dim wRa As Range
Dim HasColor: HasColor = [c2].Interior.Color ' fill C2 with desired color
Dim HasNotColor: HasNotColor = [D2].Interior.Color ' leave D2 with none or color
For Each wRa In RaLook
If wRa.HasFormula Then
wRa.Interior.Color = HasColor
Else
wRa.Interior.Color = HasNotColor
End If

Next wRa
End Sub


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.