+ Reply to Thread
Results 1 to 11 of 11

Macro Delete Duplicate Rows with Exception

Hybrid View

  1. #1
    Registered User
    Join Date
    06-15-2012
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2003
    Posts
    23

    Question Macro Delete Duplicate Rows with Exception

    I am creating a macro to delete rows based on a duplicate value in column E (see code below). However, there are times that I signify a blank cell with "--", in these instances I would like to not delete those rows because there is other information that is needed. Thanks for your help in advance.

    Private Sub RemoveDuplicateRowsSR()
    
    Dim rCell As Range
        Dim rRange As Range
        Dim lCount As Long
         
        Set rRange = Range("E8", Range("E" & Rows.Count).End(xlUp))
        lCount = rRange.Rows.Count
         
        For lCount = lCount To 1 Step -1
            With rRange.Cells(lCount, 1)
                If WorksheetFunction.CountIf(rRange, .Value) > 1 Then
                    .EntireRow.Delete
                End If
            End With
        Next lCount
    
    End Sub
    Last edited by arlu1201; 06-15-2012 at 10:57 AM. Reason: Code tags

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Macro Delete Duplicate Rows with Exception

    Do you have a sample file that you can upload?
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Registered User
    Join Date
    06-15-2012
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2003
    Posts
    23

    Re: Macro Delete Duplicate Rows with Exception

    Yes. In the attached file the "Report Example" is what I want the finished product to look like. You will notice the "--" in Cell E9 and K8. Right now I have written the code (see first post) that will go through and delete duplicate rows (based off information in column E or K). In the "Report Example" since E9 and E12 are the same ("--") then the second row will be deleted. I would like to modifiy the EA to avoid deleting duplicate "--".

    Thanks for your help.
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    05-19-2011
    Location
    San Diego, CA
    MS-Off Ver
    Excel 2010
    Posts
    33

    Re: Macro Delete Duplicate Rows with Exception

    Based on your code example and attachment, I'm following what you want to do based on Column E.
    What do you mean by "delete duplicate rows (based off information in column E or K)"?

    How does column K factor into the criteria?

  5. #5
    Registered User
    Join Date
    06-15-2012
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2003
    Posts
    23

    Re: Macro Delete Duplicate Rows with Exception

    The macro will look in the through the different worksheets for specific information that will be copied in to either Column E or Column K. The code so far is just for Column E since I will just need to expand the code, or copy it for Column K when it is working.

  6. #6
    Registered User
    Join Date
    05-19-2011
    Location
    San Diego, CA
    MS-Off Ver
    Excel 2010
    Posts
    33

    Re: Macro Delete Duplicate Rows with Exception

    Let's set aside the issue of the excluding the "--" cells, which I understand.

    Are you saying the you will delete rows with duplicates in Column E, then delete rows with duplicates in Column K as two separate steps?

    Or are you only deleting rows that have duplicates of Column E plus Column K values on the same row?

    The code for those two criteria would be different.

  7. #7
    Registered User
    Join Date
    06-15-2012
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2003
    Posts
    23

    Re: Macro Delete Duplicate Rows with Exception

    I am deleting rows will duplicates in Column E, then in the next step delete duplicates in Column K.

  8. #8
    Registered User
    Join Date
    06-15-2012
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2003
    Posts
    23

    Re: Macro Delete Duplicate Rows with Exception

    I think I figured out a way to use advance filter to get the result I want. However, when I recorded the macro and tried to run it with a buttonclick, I keep getting an error. Doing a quick search I have seen this problem come up but have not seen any workable solutions for me.

    The error is Run-time error '1004' - Paste method of Worksheet class failed

    Any thoughts?

    Sub RemoveDuplicateRows()
        Range("B8:N1000").AdvancedFilter Action:=xlFilterInPlace, Unique:=True
        Selection.Copy
        Application.CutCopyMode = False
        ActiveSheet.ShowAllData
        Selection.ClearContents
        ActiveSheet.Paste
    End Sub

  9. #9
    Registered User
    Join Date
    05-19-2011
    Location
    San Diego, CA
    MS-Off Ver
    Excel 2010
    Posts
    33

    Re: Macro Delete Duplicate Rows with Exception

    Using AdvancedFilter is a good way to remove duplicates for xl2003; however your added condition of keeping all "--" rows, makes that more difficult. To do this with AdvancedFilter, you would probably need to have two filter-copy steps or use a formula-based criteria instead with Unique=False.

    Here is some code you could try that uses a temporary column of formulas to mark those to be deleted.

    Sub Delete_Dups_with_Exception()
        Dim lCol As Long, lRow As Long, i As Long
        
        lCol = Cells.Find(What:="*", _
                After:=Cells(1, Cells.Columns.Count), _
                SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
        lRow = Range("E" & Rows.Count).End(xlUp).Row
        If lRow < 8 Then Exit Sub
        
        On Error GoTo CleanUp
        Application.ScreenUpdating = False
        Application.Calculation = xlCalculationManual
            
        With Range("E8:E" & lRow)
            '---Fill temp column with fomulas to mark rows to delete
            With Cells(.Row, lCol + 1).Resize(.Rows.Count)
                .FormulaR1C1 = "=IF(OR(RC5=""--"",MATCH(RC5,C5,0)=ROW(RC)),""Keep"","""")"
                .Value = .Value
                .SpecialCells(xlCellTypeBlanks).EntireRow.Delete
            End With
        End With
    CleanUp:
        If lCol > 0 Then Columns(lCol + 1).ClearContents
        Application.ScreenUpdating = True
        Application.Calculation = xlCalculationAutomatic
    End Sub
    This just looks for duplicates in Column E. You could repeat the process for Column K, or try to modify the formula to do it in one pass.
    Last edited by JS411; 06-18-2012 at 04:34 PM.

  10. #10
    Registered User
    Join Date
    06-15-2012
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2003
    Posts
    23

    Re: Macro Delete Duplicate Rows with Exception

    The advance filter would is actually providing the correct end results. The reason I did not want to delete the "--" in either column was because there could be a value in the other column. If I can get the advanced filter working within the macro and keeping the unique rows, then the goal would be accomplished.

    Any thoughts on the error message with the paste portion?

  11. #11
    Registered User
    Join Date
    05-19-2011
    Location
    San Diego, CA
    MS-Off Ver
    Excel 2010
    Posts
    33

    Re: Macro Delete Duplicate Rows with Exception

    The error message is because the range that was copied is no longer available to be pasted through VBA after executing the ActiveSheet.ShowAllData statement.

    One workaround is to paste the Unique Values to a temporary location.

    Here's some code that temporarily pastes the values below the data

    Sub RemoveDuplicateRows()
        Dim lRow As Long
         
        
        lRow = Cells.Find(What:="*", After:=Range("A1"), _
            SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    
        If lRow < 9 Then Exit Sub
        Application.ScreenUpdating = False
        With Range("B7:N" & lRow)  'Row 7 has headers
            .AdvancedFilter Action:=xlFilterInPlace, Unique:=True
            If .Parent.FilterMode Then
                .SpecialCells(xlCellTypeVisible).Copy
                .Offset(lRow).PasteSpecial (xlPasteValues)
                .Parent.ShowAllData
                .ClearContents
                .Offset(lRow).Resize(lRow).Copy
                .Cells(1).PasteSpecial (xlPasteValues)
                .Offset(lRow).Resize(lRow).ClearContents
            End If
        End With
        
        Application.ScreenUpdating = True
    End Sub
    Last edited by JS411; 06-20-2012 at 12:42 AM.

+ 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