Share via

In column "G" and column"Q", IF, VVS1,VVS2, VS1, VS2,SI1, SI2, I1 are mentioned multiple times rendomly. Where IF, VVS1,VVS2, VS1, VS2,SI1, SI2, I1 are in orders like A to Z. in "G" and "Q" column both may and may not exctly match. I want to highlight bet

NIRAJ SHAH 20 Reputation points
2026-02-02T10:26:38.7833333+00:00

In column "G" and column "Q", IF, VVS1,VVS2, VS1, VS2,SI1, SI2, I1 are mentioned multiple times randomly. Where IF, VVS1,VVS2, VS1, VS2,SI1, SI2, I1 are in orders like A to Z. in "G" and "Q" column both may and may not exactly match. I want to highlight better word in greater than order in green back ground and weaker in less than order in red back ground.

Microsoft 365 and Office | Excel | For education | Windows
0 comments No comments

Answer accepted by question author
  1. Rin-L 18,410 Reputation points Microsoft External Staff Moderator
    2026-02-09T11:21:07.3633333+00:00

    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. 

    User's image

    • 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. 

    User's image

    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. 

    User's image

    Looking forward to your update!


3 additional answers

Sort by: Most helpful
  1. Rin-L 18,410 Reputation points Microsoft External Staff Moderator
    2026-02-02T12:00:53.99+00:00

    Hi @NIRAJ SHAH

    Thank you for posting your question in the Microsoft Q&A forum. This is a very practical scenario, and VBA is a suitable and effective way to handle it. You can refer to the steps below to achieve the comparison and highlighting logic you described. 

    • Press Alt + F11 to open the VBA Editor 
    • Go to Insert > Module 

    User's image

    • Paste the following VBA code into the module window: 
    Option Explicit
     
    Function ClarityRank(ByVal s As String) As Long
        Dim arr
        arr = Array("IF", "VVS1", "VVS2", "VS1", "VS2", "SI1", "SI2", "I1")
        Dim i As Long
        ClarityRank = 0
        For i = LBound(arr) To UBound(arr)
            If UCase$(Trim$(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 = 1 To lastRow          
            Set rgG = ws.Cells(r, "G")
            Set rgQ = ws.Cells(r, "Q")
     
            If rgG.Value <> "" And rgQ.Value <> "" Then
                rankG = ClarityRank(rgG.Value)
                rankQ = ClarityRank(rgQ.Value)
     
                
                rgG.Interior.ColorIndex = xlNone
                rgQ.Interior.ColorIndex = xlNone
     
                If rankG > 0 And rankQ > 0 Then
                                    If rankG < rankQ Then
                        rgG.Interior.Color = vbGreen
                        rgQ.Interior.Color = vbRed
                    ElseIf rankG > rankQ Then
                        rgG.Interior.Color = vbRed
                        rgQ.Interior.Color = vbGreen
                    End If
                End If
            End If
        Next r
    End Sub
    

     

    • Close the VBA editor 
    • Press Alt + F8, select ColorClarity_G_Q, then click Run 

    User's image User's image

    Each time the data in columns G or Q changes, simply run the macro again to refresh the colors. If both values are equal, or if one of them is not a valid clarity grade, no color will be applied. 

    I hope you’ll have a chance to try the solution shared above, and I genuinely hope it proves helpful for your specific scenario. 

    If you have any updates, further questions, or would like to explore improvements or alternative approaches, please feel free to reply directly under this post. I’ll be happy to follow up and assist you further. 

    Thank you so much for reaching out 


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.  

    1 person found this answer helpful.

  2. NIRAJ SHAH 20 Reputation points
    2026-02-07T06:51:48.0133333+00:00

    [Moderator note: personal info removed] 

    0 comments No comments

  3. NIRAJ SHAH 20 Reputation points
    2026-02-04T10:31:41.1533333+00:00

    User's image


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.