VBA highlight duplicates in a column Excel Macros Examples Codes

Home/VBA/VBA highlight duplicates in a column Excel Macros Examples Codes

VBA highlight duplicates in a column Excel Macros Examples Codes: to highlight all duplicate records in a column in MS Excel 2003, 2007, 2010, 2013. We will also see the practical VBA example for highlighting the duplicates in a Column.

PREMIUM TEMPLATES LIMITED TIME OFFER

ON SALE80% OFF

50+ Project Management Templates Pack
Excel PowerPoint Word

Advanced Project Plan & Portfolio Template
Excel Template

Business Presentations Templates Pack
PowerPoint Slides

20+ Excel Project Management Pack
Excel Templates

20+ PowerPoint Project Management Pack
PowerPoint Templates

10+ MS Word Project Management Pack
Word Templates


VBA code to Highlight Duplicates in Column

Here is the Example VBA syntax and Example VBA Macro code to highlight duplicates in a Column in Excel. This will help you to know how to highlight duplicate records in a column using VBA.

VBA highlight Duplicates in Column: Procedure

Following is the VBA Syntax and sample VBA macro command to highlight duplicates in a Column of Worksheet using VBA. In this method we loop through all the records and identify the duplicates using VBA.

VBA Highlight Duplicates in a Column: Examples

The following Excel VBA macro code is to highlight duplicates in a column. This VBA macro will loop through the all the items in the first column and identify the duplicates using Match Spreadsheet Function. Here the logic is, the number is unique if the match index equals to the current processing row number. Otherwise it will be duplicate, it will highlight column in Yellow.

Sub sbHighlightDuplicatesInColumn()
Dim lastRow As Long
Dim matchFoundIndex As Long
Dim iCntr As Long
lastRow = Range("A65000").End(xlUp).Row
For iCntr = 1 To lastRow
If Cells(iCntr, 1) <> "" Then
matchFoundIndex = WorksheetFunction.Match(Cells(iCntr, 1), Range("A1:A" & lastRow), 0)
If iCntr <> matchFoundIndex Then
Cells(iCntr, 2) .Interior.Color = vbYellow
End If
End If
Next
End Sub

Instructions to run the VBA Macro code to highlight duplicates in a Column

Please follow the below steps to execute the VBA code to highlight duplicates in Column.

  • Step 1: Open any Excel workbook
  • Step 2: Press Alt+F11 – This will open the VBA Editor
  • Step 3: Insert a code module from then insert menu
  • Step 4: Copy the above code and paste in the code module which have inserted in the above step
  • Step 5: Enter some data values in Column 1. Make sure that you have some duplicate items in the data for testing purpose.
  • Now press F5 to Execute the macro and test the code

Now you can observe the code is loop thronging all the items in the column 1. And identifying the duplicates and Highlighting the duplicated cell in Yellow color if the values are repeating.

Explained VBA Code to Highlight Duplicates in a Column

Starting Macro program and sub procedure to write VBA code to highlight duplicate records in a Worksheet Column.

  • Declaring the lastRow variable as Long to store the last row value in the Column1
  • Declaring the variable MatchFoundIndex is to store the match index values of the given value
  • Declaring the variable iCntr is to loop through all the records in the column 1 using For loop
  • Highlighting the last row in the Column 1
  • looping through the column1
  • Checking if the cell is having any item, skipping if it is blank.
  • Getting match index number for the value of the cell
  • If the match index is not equals to current row number, then it is a duplicate value
  • Highlighting the duplicated cell in Yellow color

Here is the commented code explained to highlight the duplicates in a column:

Sub sbHighlightDuplicatesInColumn_C()
'Declaring the lastRow variable as Long to store the last row value in the Column1
Dim lastRow As Long
'matchFoundIndex is to store the match index values of the given value
Dim matchFoundIndex As Long
'iCntr is to loop through all the records in the column 1 using For loop
Dim iCntr As Long
'Highlighting the last row in the Column 1
lastRow = Range("A65000").End(xlUp).Row
'looping through the column1
For iCntr = 1 To lastRow
'checking if the cell is having any item, skipping if it is blank.
If Cells(iCntr, 1) <> "" Then
'getting match index number for the value of the cell
matchFoundIndex = WorksheetFunction.Match(Cells(iCntr, 1), Range("A1:A" & lastRow), 0) 
'if the match index is not equals to current row number, then it is a duplicate value
If iCntr <> matchFoundIndex Then
'Highlighting the duplicated cell in Yellow color
Cells(iCntr, 2) .Interior.Color = vbYellow
End If
End If
Next
End Sub 
LIMITED TIME OFFER
By |January 19th, 2015|VBA|3 Comments

About the Author:

PNRao is a passionate business analyst and having close to 10 years of experience in Data Mining, Data Analysis and Application Development. This blog is his passion to learn new skills and share his knowledge to make you expertise in Data Analysis (Excel, VBA, SQL, SAS, Statistical Methods, Market Research Methodologies and Data Analysis Techniques).

3 Comments

  1. Francisco Solorzano June 3, 2015 at 7:01 AM - Reply

    Buenas noches
    Thanks!
    This macro send error:
    Error of compilation
    Error of Sintaxys
    Help me?

    • PNRao June 3, 2015 at 12:32 PM - Reply

      Hi, Good morning!
      Please provide the example file to understand your data.
      Thanks-PNRao!

  2. Jason Valenzuela August 18, 2016 at 12:13 AM - Reply

    Thanks so much, this works great. I was wondering if it’s possible to highlight both/all instances of duplication. This seems to only highlight the duplicate but not the “original”, if that makes sense.

Leave A Comment


Related pages


vba database objectvba examplesis there a way to delete duplicates in excelexcel vba ribbonhow to update hyperlinks in excelpivot table vba excelexcel vba global variablesexcel application statusbaroffset formula excelvba excel dialog boxhow to use hyperlink in excel 2007vba in excel 2007 tutoriallook for duplicates in excelsample pivot tablesmsgbox vba codeexcel vba switchcollapse columns in excelhyperlink formula excelexcel vba lookuplookup function in excel 2010pivot table explanationoffset excel exampledatabase dmlexcel check for duplicateoffset formula in excel with examplems powerpoint questions and answersunlock excel sheet passwordswot analysis templates for wordhow to write insert query in excelexcel offset vlookupexcel macro select columnexcel project management spreadsheetms access nested iiffree manpower planning template excelhow to hide and unhide rows in excelexcel vba msgbox inputexcel weeks between two datesexcel vba sheetsoutlook vbavba macro excel examplesvba application.displayalertsasap utilities 64 bit downloadexcel remove sheet protectionvba opendatabasedbms interview questions and answers pdfhow to remove duplicate data in excel 2010microsoft office excel 2007 templatestimeline spreadsheet template excelexcel dynamic chartvba rowssheet activate vbaaccess vba find record in tablevba close applicationhow to use iferror function in excelduplicate sheet excelunderstanding vlookuppyramid powerpoint templatehow to delete blank rows in excelsql interview questions and answers for experienced pdfsales dashboard templatemerging excel sheets into onevba runsqlsql interview questions and answers in pdfcase select vbavba clearcreate formula in excel vbamessage box vba accesshow do i unlock an excel filesqlfunctionsunlocking excel workbookhide sheets in excelawesome excel formulasproject management plan excel templateexcel workbook password