Option Explicit
Sub Macro1()
'Written by Trebor76
'https://www.mrexcel.com/forum/excel-questions/684674-excel-macro-highlight-positive-negative-numbers.html
Dim lngStartRow As Long, lngEndRow As Long
Dim rngCell As Range, rngMyData As Range
Dim lngMyCount As Long
lngStartRow = 2 'Starting row number for the data. Change to suit.
lngEndRow = Range("D:K").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Set rngMyData = Range("D" & lngStartRow & ":K" & lngEndRow)
Application.ScreenUpdating = False
For Each rngCell In rngMyData
If Len(rngCell) > 0 And rngCell.Interior.Color = 16777215 Then
If lngMyCount > 10 Then
lngMyCount = 1
Else
lngMyCount = lngMyCount + 1
End If
Call HighlightOppositeSign(rngCell.Address, rngMyData.Address, lngMyCount)
End If
Next rngCell
Set rngMyData = Nothing
lngMyCount = 0
Application.ScreenUpdating = True
MsgBox "Process is now complete"
End Sub
Sub HighlightOppositeSign(strCellAddress As String, strDataRange As String, lngMyCount As Long)
Dim rngCell As Range, rngMyData As Range
Dim dblMyAmt As Double
dblMyAmt = CDbl(Range(strCellAddress))
For Each rngCell In Range(strDataRange)
If rngCell.Address <> strCellAddress Then
If rngCell.Value = dblMyAmt * -1 Then
If rngCell.Interior.Color = 16777215 Then
Select Case lngMyCount
Case Is = 1 'Yellow for 1st match
Range(strCellAddress).Interior.Color = RGB(255, 255, 0)
rngCell.Interior.Color = RGB(255, 255, 0)
Exit For
Case Is = 2 'Red for 2nd match
Range(strCellAddress).Interior.Color = RGB(255, 0, 0)
rngCell.Interior.Color = RGB(255, 0, 0)
Exit For
Case Is = 3 'Green for 3rd match
Range(strCellAddress).Interior.Color = RGB(0, 128, 0)
rngCell.Interior.Color = RGB(0, 128, 0)
Exit For
Case Is = 4 'Blue for 4th match
Range(strCellAddress).Interior.Color = RGB(0, 0, 255)
rngCell.Interior.Color = RGB(0, 0, 255)
Exit For
Case Is = 5 'Orange for 5th match
Range(strCellAddress).Interior.Color = RGB(255, 165, 0)
rngCell.Interior.Color = RGB(255, 165, 0)
Exit For
Case Is = 6 'Pink for 6th match
Range(strCellAddress).Interior.Color = RGB(255, 192, 203)
rngCell.Interior.Color = RGB(255, 192, 203)
Exit For
Case Is = 7 'Violet for 7th match
Range(strCellAddress).Interior.Color = RGB(238, 130, 238)
rngCell.Interior.Color = RGB(238, 130, 238)
Exit For
Case Is = 8 'Black for 8th match. Note changed the font to white or else you won't see the number
With Range(strCellAddress)
.Interior.Color = RGB(0, 0, 0)
.Font.Color = RGB(255, 255, 255)
End With
With rngCell
.Interior.Color = RGB(0, 0, 0)
.Font.Color = RGB(255, 255, 255)
End With
Exit For
Case Is = 9 'Magenta for 9th match
Range(strCellAddress).Interior.Color = RGB(255, 0, 255)
rngCell.Interior.Color = RGB(255, 0, 255)
Exit For
Case Is = 10 'Cyan for 10th match
Range(strCellAddress).Interior.Color = RGB(0, 255, 255)
rngCell.Interior.Color = RGB(0, 255, 255)
Exit For
End Select
End If
End If
End If
Next rngCell
End Sub