Excel – Automatically color cells in Excel that contain a formula

microsoft excel

How can I get Excel to automatically color cells that contain a formula?

For example, if cell B4 contains =SUM(B1:B3), cell B7 contains =B4-B7 then I would like to be able to automatically color them differently so Excel looks like this:

enter image description here

If I change cell B6 to a formula, then I expect it to automatically change color too.

Best Answer

It turns out you can use ISFORMULA with conditional formatting to do this.

From this site:

To apply conditional formatting that will highlight the cells with formulas:

  • Select cells A2:C4, with cell A2 as the active cell.
  • On the Excel Ribbon's Home tab, click the Condtional Formatting command
  • Click New Rule
  • Click Use a formula to determine which cells to format
  • Enter and ISFORMULA formula, refering to the active cell -- A2: =ISFORMULA(A2)
  • Click the Format button, and select a fill color for the cells with formulas -- gray in this example.
  • Click OK, twice, to close the windows.

Unfortunately ISFORMULA only works in Excel 2016 and above.

However, inspired by the other answers, I realised you could create some VBA to emulate that function for people on earlier versions of Excel.

To do this, you need to open up the VBA editor (Alt+F11), create a new module (menu option "Insert", then "Module") and put this code into that module:

Public Function IsFormula(ref As Range)
    IsFormula = ref.HasFormula
End Function

Save it and now the conditional formatting will work.

Related Question