VBA Code to Highlight Difference in Rows in Excel

VBA code that you can use to highlight the differences between two rows in Excel

VBA Code:

Sub HighlightRowDifferences()
    Dim rng1 As Range, rng2 As Range
    Dim cell1 As Range, cell2 As Range
    
    ' Set the ranges for the two rows you want to compare
    Set rng1 = Range("A1:E1") ' Change "A1:E1" to the first row range
    Set rng2 = Range("A2:E2") ' Change "A2:E2" to the second row range
    
    ' Clear any previous formatting
    rng1.Interior.Pattern = xlNone
    rng2.Interior.Pattern = xlNone
    
    ' Loop through each cell in the first row
    For Each cell1 In rng1
        ' Loop through each cell in the second row
        For Each cell2 In rng2
            ' Compare the values in the two cells
            If cell1.Value <> cell2.Value Then
                ' Highlight the cells with differences
                cell1.Interior.Color = RGB(255, 0, 0) ' Change color as desired
                cell2.Interior.Color = RGB(255, 0, 0) ' Change color as desired
            End If
        Next cell2
    Next cell1
    
    ' Inform the user that the highlighting is complete
    MsgBox "Differences between the rows have been highlighted!"
End Sub


Check All VBA Codes

Join Our Telegram Group techguruplus telegram group Join Our WhatsApp Group techguruplus whatsapp group
Nazim Khan - Author Image

Nazim Khan (Author) 📞 +91 9536250020
[MBA in Finance]

Nazim Khan is an expert in Microsoft Excel. He teaches people how to use it better. He has been doing this for more than ten years. He is running this website (TechGuruPlus.com) and a YouTube channel called "Business Excel" since 2016. He shares useful tips from his own experiences to help others improve their Excel skills and careers.

Leave a Comment