bottom
Great ExcelTips!
         
Your e-mail address is safe!
Close Note

Tips.Net > ExcelTips Home > Formatting > Cell Formatting > Coloring Cells with Formulas

Coloring Cells with Formulas

Summary: Want a quick way to tell where all the formulas are in a worksheet? In a few quick steps you can color all those cells. This tip explains how to do it manually, as well as providing a macro to automate the process. (This tip works with Microsoft Excel 97, Excel 2000, Excel 2002, and Excel 2003.)

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. Choose Go To from the Edit menu, or press either F5 or Ctrl+G. Excel displays the Go To dialog box.
  2. Click Special. Excel displays the Go To Special dialog box. (Click here to see a related figure.)
  3. Select the Formulas radio button.
  4. 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 follow these steps:

  1. Select the cells to which the conditional formatting should apply.
  2. Choose Conditional Formatting from the Format menu. Excel displays the Conditional Formatting dialog box.
  3. Change the left-most drop-down list to Formula Is.
  4. In the formula area (to the right of the drop-down list), enter the following formula:
     =CellHasFormula(A1)
  1. Click the Format button. Excel displays the Format Cells dialog box.
  2. Change the formatting for the cells in any way desired.
  3. Click OK to dismiss the Format Cells dialog box.
  4. Click OK to dismiss the Conditional Formatting dialog box.

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:
=GET.CELL(48,INDIRECT("rc",FALSE))
  • Click OK.
  • Now, you can follow the steps previously outlined for setting up the conditional formatting. The only difference is in step 4, where you would enter the following formula:

    =FormulaInCell
    

    Tip #2766 applies to Microsoft Excel versions: 97 | 2000 | 2002 | 2003


    PivotTables Got You Perplexed? PivotTables for the Faint of Heart shows how you can start using Excel's PivotTable tool right away to spin your data into gold! You discover how easy it really is to crunch the numbers you need to crunch. Uncover the power of creating PivotTables, editing them, formatting them, customizing them, and much more.
     
    Check out PivotTables for the Faint of Heart today!

    Helpful Links

    Ask an Excel Question
    Make a Comment

    Tips.Net Home

    ExcelTips FAQ
    ExcelTips Premium

    Learn Access Now

    Bugs and Pests Tips
    ExcelTips
    Family Tips
    Health Tips
    Home Tips
    Organizing Tips
    WordTips

    Advertise on the
    ExcelTips Site

     

    Great Info!

    Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your e-mail address and click "Subscribe."
         
    (Your e-mail address will never be shared with anyone, ever.)