Excel Macro to delete duplicate name / row

Posted on 2015-01-22
Medium Priority
Last Modified: 2015-01-23

I need your help in order to have a macro that will look into ColumnA find duplicate pc name and delete the row of the duplicate.  

Can you assist?
Question by:mldaigle1

Accepted Solution

Deadman earned 2000 total points
ID: 40564901
Press "Alt + F11" - This will open the Visual Basic Editor

Public Sub DeleteDuplicateRows()

' This macro will delete all duplicate rows which reside under
‘the first occurrence of the row.

‘Use the macro by selecting a column to check for duplicates
‘and then run the macro and all duplicates will be deleted, leaving
‘the first occurrence only.
Dim R As Long
Dim N As Long
Dim V As Variant
Dim Rng As Range

On Error GoTo EndMacro
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Set Rng = Application.Intersect(ActiveSheet.UsedRange, _
Application.StatusBar = "Processing Row: " & Format(Rng.Row, "#,##0")
N = 0
For R = Rng.Rows.Count To 2 Step -1
If R Mod 500 = 0 Then
    Application.StatusBar = "Processing Row: " & Format(R, "#,##0")
End If

V = Rng.Cells(R, 1).Value

If V = vbNullString Then
    If Application.WorksheetFunction.CountIf(Rng.Columns(1), vbNullString) > 1 Then
        N = N + 1
    End If
    If Application.WorksheetFunction.CountIf(Rng.Columns(1), V) > 1 Then
        N = N + 1
    End If
End If
Next R


Application.StatusBar = False
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
MsgBox "Duplicate Rows Deleted: " & CStr(N)

End Sub

Copy above vba code and paste and save the Excel file.
Now run the macro
LVL 48

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 40565029
If you have XL 2007 or later, you don't need a macro as the functionality is already built in...

1) Select all of your list, including the header row
2) Click "Remove Duplicates" on the Data tab of the ribbon
3) Select/Deselect columns until only Column A is left
4) Click OK.

Author Closing Comment

ID: 40566484
Thanks Deadman, this is exactly what i wanted, the macro works just fine.  I will be able to call that macro at the end of the other one for clean up purposes.

Have a good weekend,


Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question