+ Reply to Thread
Results 1 to 3 of 3

Extract unique values from filtered column

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-05-2013
    Location
    Sydney
    MS-Off Ver
    Excel 2010
    Posts
    301

    Extract unique values from filtered column

    Hi all

    The following works to extract unique values from a column.

    But, it does not work for a filtered column. I need to filter a column then get only the remaining unique values.

    Can someone suggest how to make this work please.

    Sub UniqueValues()
    Dim WSOrigin As Worksheet
    Dim dic As Object
    Dim i As Long
    Dim arrRangeArray As Variant
    Dim arrDicArray As Variant
        
        'Set sheet variables
        Set WSOrigin = Worksheets("ReportDownload")
        LastRow = WSOrigin.Range("U" & Rows.Count).End(xlUp).Row
        
        'Create a dictionary object
        Set dic = CreateObject("Scripting.Dictionary")
        dic.CompareMode = vbTextCompare
        'Pass the column to an array
        arrRangeArray = WSOrigin.Range("U2:U" & LastRow)
        'Loop
        For i = LBound(arrRangeArray) To UBound(arrRangeArray)
            If arrRangeArray(i, 1) <> "" Then dic(arrRangeArray(i, 1)) = Empty 
        Next i
    
        'Return uniques
        arrDicArray = dic.keys
        
        'Confirm
        For i = 0 To UBound(arrDicArray)
            Debug.Print arrDicArray(i)
        Next i
    
    End Sub
    Cheers

  2. #2
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Extract unique values from filtered column

    i like to use .autofilter, then you can filter (A) and get unique values with:

    Sheets(sheet1).Range("A1:A" & last).AdvancedFilter Action:=xlFilterCopy, _
    CopyToRange:=Range("BB1"), Unique:=True
    The line also copies the unique list to another range(BB)

  3. #3
    Forum Contributor
    Join Date
    03-05-2013
    Location
    Sydney
    MS-Off Ver
    Excel 2010
    Posts
    301

    Re: Extract unique values from filtered column

    Thanks for the reply.

    I needed the range to go into an array.

    I was thinking I could select a complete range of just visible cells, but it seems not to work like that. so I used the dictionary loop to do it. then this is returned to the function.

    This is what I did.

    Maybe it might help someone.

    Function UniqueFilteredRange(strColumnLetter As String) As Variant
    '************ Works (Do not mess with this) ************
    'Purpose: Returns an array with unique values.
    'The array is built by passing in the column letter into a range,
    'then loops through each visible cell in that range to create a dictionary of unique items.
    Dim dDict As Object, cCell As Range
    Dim vCellValue As Variant
    Dim rngColumn As Range
    Dim i As Integer
    
        'Set sheet variables
        Set WSOrigin = Worksheets("MySheet")
        LastRow = WSOrigin.Range(strColumnLetter & Rows.Count).End(xlUp).Row
        
        'Set the required range
        Set rngColumn = WSOrigin.Range(strColumnLetter & "2:" & strColumnLetter & LastRow)
        
        'Build dictionary with unique values
        Set dDict = CreateObject("scripting.dictionary")
        For Each cCell In rngColumn.Cells.SpecialCells(xlCellTypeVisible) ' This selects whether to enter into dictionary
           strCellValue = Trim(cCell.Value)
           If Len(strCellValue) > 0 Then
                If Not dDict.Exists(strCellValue) Then dDict.Add strCellValue, 1
           End If
        Next cCell
    
        'Return uniques to Function
        UniqueFilteredRange = dDict.keys
        
    
    End Function

+ 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. Extracting unique values from filtered column to array
    By Journeyman3000 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-13-2020, 03:22 AM
  2. Extract Unique Values from Filtered List
    By kruss777 in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 07-13-2019, 02:09 PM
  3. Extract unique text values from a filtered list
    By ljerromes in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 10-23-2015, 07:51 PM
  4. Count unique, visible values in a filtered column
    By kajakk in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 06-07-2013, 08:42 AM
  5. count unique values in filtered column
    By mcdermott2 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-09-2013, 10:40 AM
  6. Needa a formulat to count unique values in a filtered column
    By rabustam04 in forum Excel Formulas & Functions
    Replies: 11
    Last Post: 11-27-2012, 01:13 PM
  7. Count Unique Values in a Filtered Column
    By mashley in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 12-30-2008, 10:56 AM

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