+ Reply to Thread
Results 1 to 15 of 15

Macro to remove Dupes

Hybrid View

  1. #1
    Registered User
    Join Date
    02-15-2011
    Location
    Staten Island, NY
    MS-Off Ver
    Excel 2003
    Posts
    25

    Macro to remove Dupes

    I run an Access query and my results generate dupes. This is not the issue.

    I have attached a sample of this spreadsheet and in the colum marked "#" you can see the Dupes.

    Question:

    Is there a macro that can find all the dupes in this column and delete all the Duplicate rows??

    Im all out of IDEAs...as of now i do it manually

    Thanks
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Macro to remove Dupes

    hi, will that be helpful for you? There is an in-built feature for that:
    http://office.microsoft.com/en-us/ex...010342518.aspx

  3. #3
    Registered User
    Join Date
    02-15-2011
    Location
    Staten Island, NY
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: Macro to remove Dupes

    Yes...that was very helpful, But im looking to automate it. This will speed up the manual process, but in reality, I have 10 reports that i have to remove the dupes.

    Is there a macro, or some kind of code i can use?
    Last edited by Paul; 02-23-2011 at 01:09 PM. Reason: Removed quote of full post, unnecessary.

  4. #4
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,693

    Re: Macro to remove Dupes

    Try this on a copy of your workbook.

    Sub Delete_Duplicates()
        Dim i As Long
        For i = Range("B" & Rows.Count).End(xlUp).Row To 2 Step -1
            If Application.CountIf(Range("B1", "B" & i), Range("B" & i)) > 1 Then Range("B" & i).Delete Shift:=xlUp
        Next i
    End Sub
    To delete rows, change

    If Application.CountIf(Range("B1", "B" & i), Range("B" & i)) > 1 Then Range("B" & i).Delete Shift:=xlUp
    to

    If Application.CountIf(Range("B1", "B" & i), Range("B" & i)) > 1 Then Range("B" & i).EntireRow.Delete
    Last edited by jolivanes; 02-23-2011 at 01:04 PM. Reason: Add to delete complete row

  5. #5
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,887

    Re: Macro to remove Dupes

    I would suggest changing
    Then Range("B" & i).Delete Shift:=xlUp
    to
    Then Range("B" & i).EntireRow.Delete
    to delete the entire row.

    One thing to note, however, is that the values in column A are not identical when the values in column B are. (All of the other columns appear to have matching data in your sample.)

    If you don't care about the data in the other columns, and just want to keep the first instance of any dupe in column B, then the code as provided will suffice. (I'd also recommend turning off ScreenUpdating at the beginning of the code, then turning it back on at the end. This will reduce flicker and actually make the macro run faster since it doesn't have to update the screen for every row it deletes.)

    If the data in other columns does matter - and must all match in order for that row to be deleted - then another approach will be necessary.

    Hope that helps!

  6. #6
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,693

    Re: Macro to remove Dupes

    Hi Paul.
    I thought about that after I answered and corrected the code, as you see.
    We must have been doing that at the same time.
    Thanks Paul.
    John

  7. #7
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Macro to remove Dupes

    please check attachment, run macro "test"
    Attached Files Attached Files
    Last edited by watersev; 02-23-2011 at 03:56 PM.

  8. #8
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,693

    Re: Macro to remove Dupes

    watersev.
    Your code is way over my head but it also deletes Row 2 which is not a duplicate.
    John

  9. #9
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Macro to remove Dupes

    hi, jolivanes, thanks, corrected

  10. #10
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,887

    Re: Macro to remove Dupes

    Not to be overly critical of watersev's code, but that has to be one of the best attempts at thoroughly overachieving as a programmer, while at the same time ensuring that the novice user can never adjust the code without help.

    Removing duplicates from a list can be done in ONE line of code.
    Range("$A$1:$P$27").RemoveDuplicates Columns:=Array(2, 3, 4, 5, 6, 7, 8 _
            , 9, 10, 11, 12, 13, 14, 15, 16), Header:=xlYes
    This removes all duplicate rows where columns 2:16 have matching data. If you don't need to match all 15 columns, remove them from the array. If your range is not always A1:P27, get the last used row and insert that value in just one additional line of code.

    It's really not as difficult as some make it out to be.

  11. #11
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,693

    Re: Macro to remove Dupes

    watersev.
    Corrected where?

  12. #12
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,693

    Re: Macro to remove Dupes

    1. I tried watersev's code on an excel2003 computer and there it does not delete row 2. On a machine with excel2007, it does delete that row????

    2. Paul
    In order to use the one liner, one needs a UDF to run it on a 2003 machine, right?

  13. #13
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,887

    Re: Macro to remove Dupes

    In 2003 I would recommend your simple loop, with screenupdating turned off. One variable, one loop, done. Yes there are ways to do it using arrays, collections, dictionary scripting objects, etc., and some of them might be quicker on large data sets, but if the user doesn't know VBA from a hole in the wall there is little chance they can adapt the code to their real workbooks unless they are identical in structure to the sample and each other.

    I haven't searched for 'RemoveDuplicates' macros designed for 2003, but I'm sure some exist.

    These are just my opinions, your mileage may vary.

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

    Re: Macro to remove Dupes

    Hello H-Man3,

    Paul is correct that in Excel versions prior to 2007 there is no built-in method to remove duplicates. Also, he stated there are a variety of ways to accomplish the task.

    This macro uses the Dictionary object, which become available with the release of Windows 2000. I prefer this object for its speed and flexibility. Comments have been added to explain the operation of the code.

    The Attached workbook has the macro added. Use the keys Ctrl+Shift+D to run the macro. You will be prompted for the column and the starting cell of the table. Most of the code is just to get information from the user and handle potential errors.


    'Written: February 23, 2011
    'Author:  Leith Ross (www.excelforum.com0
    
    Sub RemoveDuplicates()
    
      Dim Cell As Range
      Dim Dict As Object
      Dim FirstCell As Variant
      Dim Key As String
      Dim SearchColumn As Variant
      Dim Rng As Range
      
        'Ask for the search column
         Msg = "Enter the column you want to search for duplicates. Enter either a number or letter."
        
         SearchColumn = InputBox(Msg)
           If SearchColumn = "" Then
             'User Clicked Cancel
              Exit Sub
           End If
         
        'Ask for the cell in the Upper Left Corner of the table
         Msg = "Click the cell that is in the Upper Left Corner of the Table if different than shown below."
         
         On Error Resume Next
           Set FirstCell = Application.InputBox(Prompt:=Msg, Default:="$A$1", Type:=8)
             If Err <> 0 Then
               'User Clicked Cancel
                Exit Sub
             End If
         On Error GoTo 0
         
          'Create the lookup array
           Set Dict = CreateObject("Scripting.Dictionary")
           Dict.CompareMode = vbTextCompare
          
          'Get all cells in the table
           If Rng Is Nothing Then
              MsgBox "The Table selected contains No Data."
              Exit Sub
           End If
           Set Rng = FirstCell.CurrentRegion
           
          'Convert the search column letter into a number
           SearchColumn = Cells(1, SearchColumn).Column
          
            'Clear the row if the entry in the search column is found more than once
             For Each Cell In Rng.Columns(SearchColumn).Cells
               Key = Trim(Cell.Text)
                 If Key <> "" And Not Dict.Exists(Key) Then
                    Dict.Add Key, 1
                 Else
                    Cell.EntireRow.ClearContents
                 End If
             Next Cell
          
          'Sort the table to remove the blank rows (low to high)
           Rng.Sort Key1:=Rng.Cells(1, SearchColumn), Order1:=xlAscending, _
                   Header:=xlGuess, MatchCase:=False, Orientation:=xlTopToBottom
                         
    End Sub
    Attached Files Attached Files
    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!)

  15. #15
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,693

    Re: Macro to remove Dupes

    Hi Leith.
    When I run your code in the attached workbook I get a "The table selected contains no data".
    I select column B and tried different cells in the 2nd message box.
    Where am I going wrong?
    Thanks.
    John

+ 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