A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
Hi @NIRAJ SHAH,
Thank you for your patience while I reviewed your latest notes and rechecked the workbook. Based on your description, I’ve updated the original VBA so it handles the full set of classifications in the intended order, and also accounts for #N/A values and blank cells. This should make the highlighting logic more reliable on your dataset.
Could you please try updating and running the macro with the steps below:
- Press Alt + F8 to open the Macro dialog.
- Select ColorClarity_G_Q and click Edit.
- In the editor window that opens, replace the existing code with the updated version I shared.
Option Explicit
Function ClarityRank(ByVal s As Variant) As Long
Dim arr As Variant
Dim i As Long
If IsEmpty(s) Or IsError(s) Or IsNull(s) Then
ClarityRank = 0
Exit Function
End If
On Error Resume Next
s = UCase$(Trim$(CStr(s)))
If Err.Number <> 0 Then
ClarityRank = 0
Err.Clear
Exit Function
End If
On Error GoTo 0
arr = Array("IF", "VVS1", "VVS2", "VS1", "VS2", "SI1", "SI2", "SI3", "I1", "I2", "I3")
ClarityRank = 0
For i = LBound(arr) To UBound(arr)
If s = arr(i) Then
ClarityRank = i + 1
Exit Function
End If
Next i
End Function
Sub ColorClarity_G_Q()
Dim ws As Worksheet
Dim lastRow As Long, r As Long
Dim rgG As Range, rgQ As Range
Dim rankG As Long, rankQ As Long
Set ws = ActiveSheet
lastRow = ws.Cells(ws.Rows.Count, "G").End(xlUp).Row
For r = 2 To lastRow
Set rgG = ws.Cells(r, "G") ' Projected Clarity
Set rgQ = ws.Cells(r, "Q") ' Lab Result Clarity
rgG.Interior.ColorIndex = xlNone
rgQ.Interior.ColorIndex = xlNone
If Not IsEmpty(rgG.Value) And Not IsEmpty(rgQ.Value) And _
Not IsError(rgG.Value) And Not IsError(rgQ.Value) Then
rankG = ClarityRank(rgG.Value)
rankQ = ClarityRank(rgQ.Value)
If rankG > 0 And rankQ > 0 Then
If rankG < rankQ Then
rgG.Interior.Color = RGB(144, 238, 144)
rgQ.Interior.Color = RGB(255, 182, 193)
ElseIf rankG > rankQ Then
rgG.Interior.Color = RGB(255, 182, 193)
rgQ.Interior.Color = RGB(144, 238, 144)
End If
End If
End If
Next r
End Sub
- Close the editor window.
- Return to the worksheet. Press Alt + F8, select ColorClarity_G_Q, and click Run.
I’ve tested this updated macro directly on your file, and it works as expected on my end. When you have a moment, please run through the steps above on your side and let me know the outcome.
Looking forward to your update!