VBA Code to Highlight Difference in Columns in Excel

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

VBA Code:

Sub HighlightColumnDifferences()
    Dim rng1 As Range, rng2 As Range
    Dim cell1 As Range, cell2 As Range
    
    ' Set the ranges for the two columns you want to compare
    Set rng1 = Range("A1:A10") ' Change "A1:A10" to the first column range
    Set rng2 = Range("B1:B10") ' Change "B1:B10" to the second column range
    
    ' Clear any previous formatting
    rng1.Interior.Pattern = xlNone
    rng2.Interior.Pattern = xlNone
    
    ' Loop through each cell in the first column
    For Each cell1 In rng1
        ' Loop through each cell in the second column
        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 columns 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