+ Reply to Thread
Results 1 to 7 of 7

"Unique" filter allowing duplicates

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    01-16-2012
    Location
    England
    MS-Off Ver
    MS 365
    Posts
    1,398

    "Unique" filter allowing duplicates

    Hope someone can see the flaw here.

    Attached file Duplicated Uniques.xlsm has letter combinations in Col A. Each "unique" combination needs to have a "Group number" assigned to it.

    The Macro below creates the list of "Unique" combinations in Col I, sorts them and creates sequential Group Numbers in Col J.

    But for some reason it allows two versions of one combination that you will see in Cells I18 and I19, and I can't see why?

    Option Explicit
    
    Sub Uniques()
    
    Dim a As Long, j As Long, x As Long
    
        Application.ScreenUpdating = False
    
    'Find number of rows to copy
        
        a = Range("A65536").End(xlUp).Row
    
    'Filter Unique values
        Range("I2").Select
        Range("A2:A" & a).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range( _
            "I2"), Unique:=True
        Range("J2").Select
        j = Range("I65536").End(xlUp).Row
           
    'Assign Group Names to each unique value
        ActiveCell.FormulaR1C1 = "1"
        Range("J2:J" & j).Select
        Selection.DataSeries Rowcol:=xlColumns, Type:=xlLinear, Date:=xlDay, _
            Step:=1, Trend:=False
        
    'Index/Match to Groups
        Range("B2").Select
        ActiveCell.FormulaR1C1 = "=INDEX(C[8],MATCH(RC[-1],C[7],0))"
        Range("B2").Select
        Selection.Copy
        Range("B2:B" & a).Select
        ActiveSheet.Paste
        Application.CutCopyMode = False
        
    'Convert to values
        Selection.Copy
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        ActiveSheet.Paste
        Application.CutCopyMode = False
        Range("A2").Select
        
    ' Sort by Col D
    
        Range("I2:I" & j).Select
        ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("I2"), _
            SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortTextAsNumbers
        With ActiveWorkbook.Worksheets("Sheet1").Sort
            .SetRange Range("I2:I" & j)
            .Header = xlNo
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        Range("I1").Select
    '
        Application.ScreenUpdating = True
        
    End Sub
    All suggestions received gratefully

    Ochimus

  2. #2
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: "Unique" filter allowing duplicates

    A quick help, but not a full answer to the issue can be
    
    Sub Uniques1()
    
    Dim a As Long, j As Long, x As Long
    
        Application.ScreenUpdating = False
    
    'Find number of rows to copy
        
        a = Range("A65536").End(xlUp).Row
    
    'Filter Unique values
        Call Treat
        j = Range("I65536").End(xlUp).Row
    'Assign Group Names to each unique value
        Range("J2").Select
        ActiveCell.FormulaR1C1 = "1"
        Range("J2:J" & j).Select
        Selection.DataSeries Rowcol:=xlColumns, Type:=xlLinear, Date:=xlDay, _
            Step:=1, Trend:=False
        
    'Index/Match to Groups
        Range("B2").Select
        ActiveCell.FormulaR1C1 = "=INDEX(C[8],MATCH(RC[-1],C[7],0))"
        Range("B2").Select
        Selection.Copy
        Range("B2:B" & a).Select
        ActiveSheet.Paste
        Application.CutCopyMode = False
        
    'Convert to values
        Selection.Copy
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        ActiveSheet.Paste
        Application.CutCopyMode = False
        Range("A2").Select
        
    ' Sort by Col D
    
        Range("I2:I" & j).Select
        ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("I2"), _
            SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortTextAsNumbers
        With ActiveWorkbook.Worksheets("Sheet1").Sort
            .SetRange Range("I2:I" & j)
            .Header = xlNo
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        Range("I1").Select
    '
        Application.ScreenUpdating = True
        
    End Sub
    
    Sub Treat()
    Dim ObjDic   As Object
    Set ObjDic = CreateObject("Scripting.Dictionary")
    Dim LR  As Long
    Dim WkRg  As Range
    Dim F  As Range
        Set WkRg = Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
        With ObjDic
            For Each F In WkRg
                .Item(F.Value) = Empty
            Next F
            Range("I2:I" & Range("I" & Rows.Count).End(xlUp).Row + 1).ClearContents
            Range("I2").Resize(.Count, 1) = Application.Transpose(.keys)
        End With
    End Sub
    Last edited by PCI; 05-08-2015 at 12:46 PM.
    - Battle without fear gives no glory - Just try

  3. #3
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: "Unique" filter allowing duplicates

    For some reason the advanced filter insists on working with a header included. So... you when you filter the range starting at row 2 (so skipping the real header), whatever the first record is will be copied both as a header and (if there is another instance), also as a unique value.

    I suggest you change this line:
    Range("A2:A" & a).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range( _
            "I2"), Unique:=True
    to this (it will include the header in the copy to the unique list, but paste into I1 instead of 2, then since you sort I2 and down, your header will stay put and your list will be unique
    Range("A1:A" & a).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range( _
            "I1"), Unique:=True
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  4. #4
    Valued Forum Contributor
    Join Date
    01-16-2012
    Location
    England
    MS-Off Ver
    MS 365
    Posts
    1,398

    Re: "Unique" filter allowing duplicates

    Arkadi,

    Many thanks for the prompt response and clear explanation.

    There MAY come a day when I get to the bottom of Excel's wondrous wrinkles - but it's looking increasingly unlikely

    Ochimus

  5. #5
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: "Unique" filter allowing duplicates

    Ochimus,
    I have yet to meet someone who has achieved that goal.... though some are incredibly close (and I am certainly not one of them). Happy to have been of use

  6. #6
    Valued Forum Contributor
    Join Date
    01-16-2012
    Location
    England
    MS-Off Ver
    MS 365
    Posts
    1,398

    Re: "Unique" filter allowing duplicates

    PCI,

    Not sure what happened, because when I opened the thread a few moments ago, your response just said "Wrong Answer".

    Just refreshed it, and now see your suggested approach, so many thanks for that as well,

    Ochimus

  7. #7
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: "Unique" filter allowing duplicates

    Well I proposed a wrong answer first and then got a new one.
    For the fun a remake of the macro.
    BTW it looks to me that the way your macro is preparing the group formula is done a bit too early and then the group is not correct
    "Sort by Col D " is done at the end and only for column I

    
    Sub Uniques2()
    Dim objSortedList  As Object
    Set objSortedList = CreateObject("System.Collections.Sortedlist")
    Dim F  As Range
    Dim i As Long
        With objSortedList
            For Each F In Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
                If (Not (.Contains(F.Value))) Then .Add F.Value, Empty
            Next F
            Range("I2:J" & Range("I" & Rows.Count).End(xlUp).Row + 1).ClearContents
            For i = 0 To .Count - 1
                Range("I2").Offset(i, 0) = .GetKey(i)
                Range("I2").Offset(i, 1) = i + 1
            Next i
        End With
        With Range("B2:B" & Range("A" & Rows.Count).End(xlUp).Row)
            .FormulaR1C1 = "=INDEX(C[8],MATCH(RC[-1],C[7],0))"
            .Value = .Value
        End With
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 6
    Last Post: 01-30-2015, 03:13 PM
  2. Replies: 0
    Last Post: 11-22-2013, 01:36 PM
  3. Replies: 2
    Last Post: 08-17-2012, 05:10 AM
  4. Replies: 1
    Last Post: 07-16-2010, 02:44 AM
  5. "Criteria Range" in the "Data/Filter/Advanced Filter" to select Du
    By TC in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 05-11-2005, 10:06 PM

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