+ Reply to Thread
Results 1 to 5 of 5

Populate a Listbox with Unique data of filtered range

Hybrid View

  1. #1
    Forum Contributor HaroonSid's Avatar
    Join Date
    02-28-2014
    Location
    india
    MS-Off Ver
    Excel 2013
    Posts
    2,095

    Populate a Listbox with Unique data of filtered range

    hi,
    i am using below code to populate data with unique data of range f10:f1000

    Private Sub UserForm_Initialize()
    
        Dim arrUnqItems As Variant
    
        With Sheets("SHEET1")
            .Range("F10", .Cells(.Rows.Count, "F").End(xlUp)).AdvancedFilter xlFilterCopy, , .Cells(1, .Columns.Count), True
            arrUnqItems = Application.Transpose(.Range(.Cells(2, .Columns.Count), .Cells(.Rows.Count, .Columns.Count).End(xlUp)).Value)
            .Columns(.Columns.Count).Clear
        End With
    
        Me.ListBox1.Clear
        Me.ListBox1.List = arrUnqItems
    
        Erase arrUnqItems
    
    End Sub
    i want to change above code to apply at filtered data

    i want to show unique values of filtered data only

    thank you
    Attached Files Attached Files
    Use Code-Tags for showing your code :
    Please mark your question Solved if there has been offered a solution that works fine for you
    If You like solutions provided by anyone, feel free to add reputation using STAR *

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

    Re: Populate a Listbox with Unique data of filtered range

    From what I see the listbox already shows only unique values?
    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.

  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: Populate a Listbox with Unique data of filtered range

    HaroonSid,

    If what you mean is you only want to see unique values on the sheet, then add

    .Range("F9", .Cells(.Rows.Count, "F").End(xlUp)).AdvancedFilter xlFilterInPlace, unique:=True
    just before the "End With" line in the userform initialize code

  4. #4
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Populate a Listbox with Unique data of filtered range

    If you want the listbox click code to only select visible cells too, I would suggest this
    Private Sub ListBox1_Click()
        With ListBox1
            Sheets("SHEET1").Range(.List(.ListIndex, 1)).Select
        End With
    End Sub
    
    Private Sub UserForm_Initialize()
    
        Dim arrUnqItems As Variant
        Dim data As Range
    
        With Sheets("SHEET1")
            Set data = .Range("F10", .Cells(.Rows.Count, "F").End(xlUp))
        End With
    
        With Me.ListBox1
            .ColumnCount = 2
            .ColumnWidths = ";0"
            .List = UniqueData(data)
        End With
    
    End Sub
    
    Function UniqueData(ByVal dataRange As Range)
        Dim cell As Range
        Dim d As Object
        Dim x As Long
        On Error Resume Next
        Set dataRange = dataRange.SpecialCells(xlCellTypeVisible)
        On Error GoTo 0
        If Not dataRange Is Nothing Then
            Set d = CreateObject("scripting.dictionary")
            For Each cell In dataRange.Cells
                If Not d.exists(cell.Value) Then d(cell.Value) = cell.Address
            Next
            UniqueData = Application.Transpose(Array(d.keys, d.items))
        End If
        
    End Function
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  5. #5
    Forum Contributor HaroonSid's Avatar
    Join Date
    02-28-2014
    Location
    india
    MS-Off Ver
    Excel 2013
    Posts
    2,095

    Re: Populate a Listbox with Unique data of filtered range

    @ xlnitwit
    Thank you very very much

    best regards
    Mohd Haroon

+ 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. [SOLVED] Populate unique listbox values from combobox selection
    By terriertrip in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-29-2017, 05:34 PM
  2. Populate ListBox with data from named range
    By kaseyleigh in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-12-2016, 12:06 PM
  3. Populate ListBox with Filtered Range
    By smartbuyer in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-21-2015, 01:09 AM
  4. [SOLVED] Populate a listbox with unique values from a range on a worksheet
    By D.Lovell in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-27-2014, 04:18 PM
  5. Populate a textbox on Userform with data from a filtered range using VBA
    By newbi004 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-13-2014, 04:20 AM
  6. [SOLVED] populate listbox with unique text
    By mcdermott2 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 04-02-2013, 03:50 PM
  7. Setting up a validation of data listbox to provide the unique items within a range
    By jedale@gmail.com in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 07-30-2006, 04:10 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