VBA find duplicate values in a column

Home/VBA/VBA find duplicate values in a column

VBA find duplicate values in a column Excel Macros Examples Codes: to find all duplicate records in a column in MS Excel 2003, 2007, 2010, 2013. We will also see the practical VBA example for finding 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 Remove Duplicates in ListBox

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

VBA find Duplicates in Column: Procedure

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

VBA Find Duplicates in a Column: Examples

The following Excel VBA macro code is to find duplicate values 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 print the “duplicate” in the second column.

Sub sbFindDuplicatesInColumn()
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) = "Duplicate" 
End If
End If
Next
End Sub 

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

Please follow the below steps to execute the VBA code to delete 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 printing the label as “Duplicate” in the Column B if the value is repeating.

Explained VBA Code to Find Duplicates in A Column

Starting Macro program and sub procedure to write VBA code to find 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
  • Finding 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
  • Printing the label in the column B

Here is the commented VBA Macro code, explained the procedure by each statement.

Sub sbFindDuplicatesInColumn_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
'Finding 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
'Printing the label in the column B
Cells(iCntr, 2) = "Duplicate" 
End If
End If
Next
End Sub 
LIMITED TIME OFFER
By |January 19th, 2015|VBA|4 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).

4 Comments

  1. Sohail May 16, 2015 at 3:49 PM - Reply

    Great explanation…. U People really God ‘s Own Creation

    Thanks A Lot

    Sohail Imran

  2. Suresh August 25, 2016 at 8:57 PM - Reply

    Hi Team,

    I have a question : i have given the some count in one of excel cell, now i need to find that data input cell in vba coding. could you please advise how to write this coding.

    Regards,
    Suresh

  3. siamak mahdikar February 1, 2017 at 2:51 AM - Reply

    Hi,
    Thanks a lot,
    I am from Iran, so I do not speak english very vell . excuse me .
    I have one question .
    How I can change column B to each coloumn ? Can I set this macro to any column ? for example run macro ic column E and F ?
    please help me .

  4. iel February 5, 2017 at 2:38 AM - Reply

    what if i want only find duplicates for all the same that i input to the textbox in userform

    i hope this problem have a solution

    thank you in advance

Leave A Comment


Related pages


vlookup in excel definitionexcel macro open worksheetexcel macro protect sheet with passwordlinking excel cellsvb rename fileexcel vba data typesexcel vba sql query exampleend vbavba advanced tutorialexcel vba integer rangewhat is vlookup function in excelexcel kpi dashboard templatesrept in excelhow to search for blank cells in excelprompt vbaexcel chart titleduplicates in excel 2003excel vba data validationvba excel match functiongantt chart excel 2003 templateexcel unshare workbooksql data analyst interview questions and answersvba excel delete columnshow to protect a excel workbookexcel vba matrixadvanced excel pivot tablehow do you merge cells in excel 2010vba vbnewlinesort in ascending order excelexcel team schedule templatesorting cells in excelwhat is hlookupautofit in excel 2010clear sheet vbahow to unhide all hidden rows in excelaccess vba function return valueusedrange excelexcel vba substitutecheck box macroms access find record vbapivot table excel templatehow to learn excel vbafind duplicate row in excelvba programming coursehighlight duplicate cells in excelchange a row to a column in excelhow to unhide all sheetsvba for excel examplesend loop vbasas basics tutorialsauto fit row heighthow to run an excel macrosum vlookupinsert drop down box in excel 2007excel vba programming examples pdfprotect excel worksheetexcel vba language referencehow to use a pivot table in excel 2007multiple project tracking template excelprotected workbook forgot passwordmicrosoft excel multiple if statementsvlookup commandlearning vbaexample of dmlhow to use iferror in excel 2010what is excel ribbonexcel vba open txt filehide excel formulasvba excel selectwindows activate vbaexcel if statement with 2 conditionshow to merge a cell in excelexcel 2007 paste specialunprotect excel 2010 without passworddrop down list excel 2007excel macro loop