+ Reply to Thread
Results 1 to 5 of 5

Deleting Duplicate Rows

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-13-2009
    Location
    Australia
    MS-Off Ver
    Excel 2016
    Posts
    245

    Deleting Duplicate Rows

    Hello, could someone please help me with the following:

    In my Excel 2003 worksheet I need a macro to search column B for duplicate entries.

    The data in column B is both numeric and string.

    If there are any duplicate entries, then I need the duplicate rows to be deleted.

    If any one could please help - it would be greatly appreciated.

    Kind regards,

    Chris
    Last edited by longbow007; 10-15-2009 at 01:00 AM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Deleting Duplicate Rows

    Hello Chris,

    Post a few examples of the column "B" strings. Alphanumeric strings should not present a problem when identifying duplicates.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Deleting Duplicate Rows

    Here are two of my existing macros for this task, perhaps one will suit you when adjusted. The first one should be faster on large datasets:

    Sub DeleteDupesOnly()
    'Deletes duplicates leaving complete list of unique values only
    'Column B is evaluated
    Dim LR As Long
    
    'Data needs to start at row 2, so we'll insert a row
        Rows(1).Insert (xlShiftDown)
        LR = Range("B" & Rows.Count).End(xlUp).Row
        Range("AA2:AA" & LR).FormulaR1C1 = "=COUNTIF(R2C2:RC2,RC2)"
        Range("AA1") = "Key"
        Range("AA1").AutoFilter Field:=1, Criteria1:=">1"
        Range("AA2:AA" & LR).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        Rows(1).Delete (xlShiftUp)
        Columns("AA:AA").Clear
    End Sub
    
    
    Sub DeleteDupesByLoop()
    Dim LR As Long, MyCount As Long
    LR = Range("B" & Rows.Count).End(xlUp).Row
    
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    
    For i = LR To 2 Step -1
        MyCount = WorksheetFunction.CountIf(Cells(i, "B"), Columns("B:B"))
        If MyCount > 1 Then Rows(i).EntireRow.Delete Shift:=xlUp
        MyCount = 0
    Next i
    
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Deleting Duplicate Rows

    Hi Chris,

    Use a helper column, say C. Assuming your data is in B1:B100 enter in C1

    =COUNTIF($B1:$B100,B1)
    and copy this down column C.

    Now filter on column C for values > 1 which represent those rows which have duplicate values lower down the list.

    HTH
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  5. #5
    Forum Contributor
    Join Date
    06-13-2009
    Location
    Australia
    MS-Off Ver
    Excel 2016
    Posts
    245

    Re: Deleting Duplicate Rows

    thanks JBeaucaire, Leith Ross and Richard Buttrey for all your excellent help and assistance - it is very much appreciated.

    Kind regards,

    Chris

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1