|
|
|
|
|
|
|
|
Excel
|
Highlight all formula cells on the activesheet that contain both cell refs and constants
|
|
|
Ease of Use
|
Easy
|
|
Version tested with
|
2003
|
|
Submitted by:
|
brettdj
|
|
Description:
|
Any formula cell on the activesheet that contains a reference to another cell AND a constant is highlighted with a green background
|
|
Discussion:
|
It's quite common for people to hardcode "adjusting" or "balancing" items into cells to force particular outcomes, especially with financial statements. These balances can hide errors, or even worse prevent data reconciling if they aren't removed. This code provides a flagging test to highlight potential "adjusting" items.
|
|
Code:
|
instructions for use
|
Option Explicit
Sub FormatMixedConstants()
Dim RegEx As Object, RegCol As Object, RegMat As Object
Dim CountNumeric As Integer
Dim NumMatches As Long
Dim TestRange As Range
Dim Cel As Range
On Error Resume Next
Set TestRange = ActiveSheet.Cells.SpecialCells(xlCellTypeFormulas)
On Error Goto 0
'If there are no formulas then exit sub
If TestRange Is Nothing Then Exit Sub
Application.ScreenUpdating = False
' The on error is a deliberate method to handle the non-existence of cell precedents
On Error Resume Next
Set RegEx = CreateObject("vbscript.regexp")
With RegEx
.Global = True
.Pattern = "[^=+-/*^()]+"
End With
For Each Cel In TestRange
Set RegCol = RegEx.Execute(Cel.Formula)
' Parse all formulas and look at text in between "+" .. "-" .. "*" .. "(" etc
NumMatches = RegCol.Count
If NumMatches > 0 Then
For Each RegMat In RegCol
' If a discrete number is found then exit cell parsing
If IsNumeric(RegMat) Then
CountNumeric = 1
Exit For
End If
Next RegMat
Set RegCol = Nothing
'if there are no cell precedents or no numeric cells then skip formatting
If Not (CountNumeric > 0) Or Not (Cel.DirectPrecedents.Count > 0) Then
'remove green formatting from forumla if cell has no constants
If Cel.Interior.ColorIndex = 35 Then Cel.Interior.ColorIndex = xlNone
Else
' Format cells with numbers and cell references as green
Cel.Interior.ColorIndex = 35
End If
CountNumeric = 0
End If
Next Cel
Application.ScreenUpdating = True
Set RegCol = Nothing
Set RegEx = Nothing
Set TestRange = Nothing
End Sub
|
|
How to use:
|
- Copy the code above.
- Open your workbook.
- Hit Alt+F11 to open the Visual Basic Editor (VBE).
- From the menu, choose Insert-Module.
- Paste the code into the code window at right.
- Close the VBE, and save the file if desired.
|
|
Test the code:
|
- Run the macro by going to Tools-Macro-Macros and double-click FormatMixedConstants
|
|
Sample File:
|
HighlightMixedCells(KB24).zip 13.98KB
|
|
Approved by mdmackillop
|
|
This entry has been viewed 83 times.
|
|
|