Consulting

Results 1 to 15 of 15

Thread: Filter Rows with Duplicate values.

  1. #1
    VBAX Contributor
    Joined
    Apr 2010
    Posts
    182
    Location

    Filter Rows with Duplicate values.

    Hi,

    The following code deletes the duplicate rows from the worksheet.

    How could I change it so that it filters the duplicate rows instead of deleting.
    Sub FilterDups()
    Dim x               As Long
    Dim LastRow         As Long
        LastRow = Range("A1048756").End(xlUp).Row
        For x = LastRow To 1 Step -1
            If Application.WorksheetFunction.CountIf(Range("A1:A" & x), Range("A" & x).Text) > 1 Then
                Range("A" & x).EntireRow.Delete
            End If
        Next x
    End Sub
    Any help on this would be kindly appreciated.
    Best Regards,
    adamsm

  2. #2
    VBAX Contributor
    Joined
    Apr 2010
    Posts
    182
    Location
    I did try by adding the following line
    Dim rng As Range
    Set rng = ActiveSheet.AutoFilter.Range
    But it was of no hope. and it didn't make any sense.
    Best Regards,
    adamsm

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Add a helper column which will show the duplicates and filter on that.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    VBAX Contributor
    Joined
    Apr 2010
    Posts
    182
    Location
    Thanks for the reply mdmackillop.

    If I may ask, how could that be done.

    Thanks in advance
    Best Regards,
    adamsm

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Sub FilterDups()
    Dim LastRow As Long
    Dim rng As Range

    LastRow = Range("A" & Rows.Count).End(xlUp).Row
    Columns(1).Insert
    Range("A1").Value2 = "Filter"
    Range("A2").Resize(LastRow - 1).Formula = "=COUNTIF($B$1:$B$" & LastRow & ",B2)>1"
    Set rng = Range("A1").Resize(LastRow)
    rng.AutoFilter field:=1, Criteria1:="TRUE"
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Example
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    VBAX Contributor
    Joined
    Apr 2010
    Posts
    182
    Location
    Thanks for the reply & help mdmackillop & xld.

    To understand my problem better I have attached a sample workbook.

    The present code only hides the unique rows displaying the duplicate rows with the macro code FilterAllDuplicateRows(). And the duplicate rows get deleted with the macro DeleteAndClearFilter()

    What I’m trying to get help is to display the data in the list box, as the sheet hides all the unique rows displaying the duplicate rows when the command button Search Dup is clicked. And also display the data in the list box when macro deletes the duplicate rows and Unhides the unique rows on click of the command button Del Dups.

    In short, how do I filter and unfilter the rows simultaneously from the list box and the sheet with the command buttons?
    Best Regards,
    adamsm

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Let me get this right. Do you want to only show unqiue values in the lastbox, or do you only want to show each item only once in the listbox, or something else?

    And then what?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  9. #9
    VBAX Contributor
    Joined
    Apr 2010
    Posts
    182
    Location
    Thanks for the reply Xld. Here's a better version.

    In this version when the user clicks the search dup button the listbox and the worksheet shows only the duplicate rows.

    This is fine with me.

    When the command button del dup is clicked the code deletes the dup rows and unhides the unique rows.

    but this unhiding is not seen in the listbox. When the worksheet unhides the rows I'm trying to show all the unhidden rows on the listbox simulatenously with the worksheet.

    I hope I have made my question clear.
    Best Regards,
    adamsm

  10. #10
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Private Sub cmdDelete_Click()
    Call DeleteAndClearFilter
    Cells.Rows.Hidden = False
    Call FilterList
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  11. #11
    VBAX Contributor
    Joined
    Apr 2010
    Posts
    182
    Location
    Thanks for the code xld.

    It seems that the following code also does delete & unhide the rows both form the worksheet & the Listbox.
    Private Sub cmdDelete_Click()
        Dim x               As Long
        Dim LastRow         As Long
        LastRow = Range("A" & Rows.Count).End(xlUp).Row
        For x = LastRow To 6 Step -1
        If Application.WorksheetFunction.CountIf(Range("A6:A" & x), Range("A" & x).Text) > 1 Then
        Range("A" & x).EntireRow.Delete
            End If
        Next x
        Range("A6", "A" & LastRow).EntireRow.Hidden = False
        FilterList
    End Sub
    Best Regards,
    adamsm

  12. #12
    VBAX Contributor
    Joined
    Apr 2010
    Posts
    182
    Location
    Meanwhile how could I display a message like "from a total of 15 rows 4 duplicate entries were found" in the userform title bar with the click of the search dups button( if duplicate rows were found). And if there are no duplicate values "no duplicate values found".
    Best Regards,
    adamsm

  13. #13
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    This is a good example of "Project Creep". Please be clear from the outset what your requirements are. I suspect there is a lot of wasted time and effort here.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  14. #14
    VBAX Contributor
    Joined
    Apr 2010
    Posts
    182
    Location
    mdmackillop, if you label what I had asked as "a good example of Project Creep"; Nevermind. I'll try to figure out what it for myself. Meanwhile, for the rest of the forum users I guess my workbook would give some help to assist them in dealing with duplicate data entries.

    Anyways, Once again thanks for all the help & support that had been given me in this thread.
    Best Regards,
    adamsm

  15. #15
    VBAX Contributor
    Joined
    Apr 2010
    Posts
    182
    Location
    Once the problem is solved Ill try to post the completed workbook.
    Anyways, Once again thanks for all the help & support that had been given me in this thread.
    Last edited by adamsm; 06-28-2010 at 08:44 AM. Reason: reply Repeated
    Best Regards,
    adamsm

Posting Permissions

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