VBA Code to Highlight Duplicate Values in a Column in Excel

VBA Code:

Sub HighlightDuplicates()
    Dim rng As Range
    Dim cell As Range
    Dim dict As Object
    
    ' Set the range of cells to check for duplicates
    Set rng = Range("A1:A100") ' Change the range as desired
    
    ' Create a dictionary object to store the unique values
    Set dict = CreateObject("Scripting.Dictionary")
    
    ' Loop through each cell in the range
    For Each cell In rng
        ' Check if the value already exists in the dictionary
        If dict.Exists(cell.Value) Then
            ' Highlight the cell as a duplicate
            cell.Interior.Color = RGB(255, 0, 0) ' Change the color as desired
        Else
            ' Add the value to the dictionary
            dict.Add cell.Value, 0
        End If
    Next cell
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