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


how to do data validation in excel 2007delete blanks in excelhow to create checkbox in excelexcel gantt chart macroxls to xml conversionvba to refresh pivot tablerows vbavba sql selectdashboard excel examplesdata validation in excel 2010 tutorialexcel vba programming examplesvba last rowunprotect all sheetsfso copyfilehow to use a pivot table in excel 2013loop excel macrodashboards for exceldml querygantt chart excel 2003 templatehow to merge rows in excelhow do i unhide rows in excelrept functionvba workbooks openexcel vba vlookup formulawhat is a stock chart in excelno of rows and columns in excel 2007sample vbscript codesimple pie chart worksheetvba dim string arraycool excel spreadsheetsdeclare global variable vbams excel hyperlinkmicrosoft excel adding columnsvlookup definition wikipediacombobox vbaconstruction estimate template excelexcel advanced filter criteria rangechecklist templates excelvlookup microsoft exceldashboard template powerpointunprotect excel 2010 workbook without passwordprotect formulas in excelvba user defined functioncommands of ddlglobal variables vbanested if excel 2007how do you merge cells in excelautosave excel 2003open and activate workbook vbarefresh pivot table in vbaclose msgbox vbavbs copy folderiserror excel functionsql question and answer pdfexcel insert commentvb sample programs pdfhow to unprotect excel workbook without knowing passwordremove duplicates in excel 2007iserror excel formulaunhide excel columnaccess vba read text filevba for microsoft wordvba selection.deletelookup functionsiserror function in excelvba tutorial excel 2007excel macro usescombo box excel 2007excel vba blankc language interview questions and answers pdf