|
|
|
|
|
|
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 TestRange Is Nothing Then Exit Sub
Application.ScreenUpdating = False
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)
NumMatches = RegCol.Count
If NumMatches > 0 Then
For Each RegMat In RegCol
If IsNumeric(RegMat) Then
CountNumeric = 1
Exit For
End If
Next RegMat
Set RegCol = Nothing
If Not (CountNumeric > 0) Or Not (Cel.DirectPrecedents.Count > 0) Then
If Cel.Interior.ColorIndex = 35 Then Cel.Interior.ColorIndex = xlNone
Else
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 123 times.
|
|