Highlight Cells with Formulas
This macro will highlight all of the cells in a worksheet which contain a formula. The first one listed will highlight all of the cells with formulas within the active worksheet. The second macro listed will highlight all of the cells which contain a formula which are also within a predefined range of cells; to change this range simply change the cell references in this line of code
For Each Rng In Range("A1:B25").
These macros, when run, will remove any previous color from the worksheet. So, if you have a very colorful worksheet and want to keep it that way, do not use this macro.
This macro is best used for when you have a large number of formulas and you're not sure if you know where they all are or if you inherit or receive worksheets from other people and you want to quickly locate all formulas.
To change the color of the highlight for the first macro, change the number in this line of code
If r.HasFormula Then r.Interior.ColorIndex = 6 'yellow.
To change the color of the highlight for the second macro, change the number in this line of code
Rng.Interior.ColorIndex = 3 'red.
Highlight Cells with Formulas - Works on entire active worksheet
Sub Highlight_Formulas()
'Highlights all cells with formulas on the active sheet
'Will remove color from cells without formulas
Dim r As Range
With ActiveSheet.UsedRange
.Interior.ColorIndex = xlNone
For Each r In .Cells
If r.HasFormula Then r.Interior.ColorIndex = 6 ‘yellow
Next
End With
End Sub
Highlight Cells with Formulas - Works on Predefined Range of Cells
Sub Highlight_ Formulas_Range()
'Highlights all cells with formulas within a selected range
'Will remove color from cells without formulas
Dim Rng As Range
For Each Rng In Range("A1:B25") 'Range to highlight cells
If Rng.HasFormula Then
Rng.Interior.ColorIndex = 3 'red
Else
Rng.Interior.ColorIndex = 0 'blank
End If
Next Rng
End Sub
How to Install the Macro
- Select and copy the text from within the grey box above.
- Open the Microsoft Excel file in which you would like the Macro to function.
- Press "Alt + F11" - This will open the Visual Basic Editor - Works for all Excel Versions.
Or For other ways to get there, Click Here.
For Excel Versions Prior to Excel 2007
Go to Tools > Macros > Visual Basic Editor
For Excel 2007
Go to Office Button > Excel Options > Popular > Click Show Developer tab in the Ribbon. Then go to the Developer tab on the ribbon menu and on the far left Click Visual Basic
- On the new window that opens up, go to the left side where the vertical pane is located. Locate your Excel file; it will be called VBAProject (YOUR FILE'S NAME HERE) and click this.
- If the Macro goes in a Module, Click Here, otherwise continue to Step 8.
- Go to the menu at the top of the window and click Insert > Module
- Another window should have opened within the Visual Basic Editor's window. Within this new window, paste the macro code. Make sure to paste the code underneath the last line of anything else that is in the window.
- Go to Step 8.
- If the Macro goes in the Workbook or ThisWorkbook, Click Here, otherwise continue to Step 8.
- Directly underneath your excel file called VBAProject(your file's name here), click the Microsoft Excel Objects folder icon to open that drop-down list.
- Then, at the bottom of the list that appears, double-click the ThisWorkbook text.
- A new window inside the Visual Basic Editor's window will appear. In this new window, paste the code for the macro. Make sure to paste this code underneath the last line of any other code which is already in the window.
- Go to Step 8.
- If the Macro goes in the Worksheet Code, Click Here, otherwise continue to Step 8.
- Directly underneath your excel file called VBAProject(your file's name here), click the Microsoft Excel Objects folder icon to open that drop-down list.
- Within the list that appears you will see every worksheet that is in that excel file. They will be listed as such: Sheet1(NAME OF SHEET HERE) and under that will be Sheet2(NAME OF SHEET HERE). Select the sheet in which you want the macro to run and double-click that sheet.
- A new window inside the Visual Basic Editor's window will appear. In this new window, paste the code for the macro. Make sure to paste this code underneath the last line of any other code which is already in the window.
- Repeat steps b and c for every sheet you want the macro to work in. Putting the macro in one sheet will not enable it for any other sheets in the workbook.
- Go to Step 8.
- Close the Microsoft Visual Basic Editor window and save the Excel file. When you close the Visual Basic Editor window, the regular Excel window will not close.
- You are now ready to run the macro.