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

Join the TechGuruPlus Telegram group

Join Our WhatsApp Group

Join the TechGuruPlus WhatsApp group
Nazim Khan, founder of TechGuruPlus.com

— Founder & Editor, TechGuruPlus.com
[MBA in Finance]

Nazim Khan has spent over ten years working with Excel, Tally and office documentation in accounts, finance and MIS roles. He has been running TechGuruPlus.com since 2016, where he publishes free editable templates for office work, and the YouTube channel Business Excel. He has trained more than 50,000 people online. Every template on this site is built and checked by him before it is published.

Contact  Â·  Excel Course

Leave a Comment