Sub HighlightComparableRows_ActiveWorkbook()
Dim ws As Worksheet, lastItemRow As Integer, i As Integer, j As Integer, subjectProfit As Double
Dim volume As Double, valueM As Double, valueN As Double, withinRange As Boolean, noHighlightRow As Integer
Dim foundGreen As Boolean, foundBlue As Boolean, activeWb As Workbook
Set activeWb = ActiveWorkbook
For Each ws In activeWb.Worksheets
If Application.WorksheetFunction.CountA(ws.Cells) = 0 Then GoTo NextSheet
ws.Range("A4:V4").AutoFilter
subjectProfit = ws.Range("Q2").Value
valueM = ws.Range("M2").Value
valueN = ws.Range("N2").Value
lastItemRow = ws.Cells(ws.Rows.Count, 21).End(xlUp).Row
ws.Range("A4:V" & lastItemRow).Sort Key1:=ws.Range("U5"), Order1:=xlDescending, Header:=xlYes
ws.Range("J5:J" & lastItemRow).NumberFormat = "#,##0"
ws.Range("K5:K" & lastItemRow).NumberFormat = "#,##0"
foundGreen = False
foundBlue = False
noHighlightRow = 0
For i = 5 To lastItemRow
If ws.Range("H2").Value = "C" Then
volume = ws.Cells(i, 11).Value
If volume < 10000 Or (volume >= 10000 And volume < 50000 And volume < valueN) Or _
(volume >= 50000 And volume < 100000 And volume < valueN) Or _
(volume >= 100000 And volume < 1000000 And volume < valueN) Or _
(volume >= 1000000 And volume < valueN) Then
ws.Cells(i, 11).Interior.Color = RGB(255, 192, 192)
End If
End If
If ws.Range("F2").Value = "C" Then
volume = ws.Cells(i, 10).Value
If volume < 10000 Or (volume >= 10000 And volume < 50000 And volume < valueM) Or _
(volume >= 50000 And volume < 100000 And volume < valueM) Or _
(volume >= 100000 And volume < 1000000 And volume < valueM) Or _
(volume >= 1000000 And volume < valueM) Then
ws.Cells(i, 10).Interior.Color = RGB(255, 192, 192)
End If
End If
If InStr(1, ws.Cells(i, 4).Value, ws.Range("G2").Value, vbTextCompare) > 0 Then
ws.Cells(i, 4).Interior.Color = RGB(255, 192, 192)
End If
If ws.Cells(i, 22).Value < 50 Then
ws.Cells(i, 22).Interior.Color = RGB(255, 192, 192)
End If
Next i
For i = 5 To lastItemRow
withinRange = True
For j = 10 To 22
If ws.Cells(i, j).Interior.Color = RGB(255, 192, 192) Then
withinRange = False
Exit For
End If
Next j
If withinRange Then
If ws.Cells(i, 21).Value >= subjectProfit Then
ws.Rows(i).Interior.Color = RGB(192, 255, 192)
foundGreen = True
Exit For
End If
If Not foundGreen And Abs(ws.Cells(i, 21).Value - subjectProfit) <= 0.0005 Then
ws.Rows(i).Interior.Color = RGB(176, 196, 222)
foundBlue = True
Exit For
End If
If Not foundGreen And Not foundBlue And noHighlightRow = 0 Then
noHighlightRow = i
End If
End If
Next i
If Not foundGreen And Not foundBlue And noHighlightRow > 0 Then
ws.Rows(noHighlightRow).Interior.Color = RGB(255, 160, 160)
End If
NextSheet:
Next ws
MsgBox "Highlighting completed for all sheets."
End Sub