Populating a list box with unique values from a column

charlie1105

Board Regular
Joined
Oct 16, 2007
Messages
182
Hi all,

I'm trying to populate a listbox with all the unique items in a column. By unique I mean that if there is a duplicate that item will only appear once in the listbox. The range is quite large (10,000 rows), but there will only be about 10 different items in the range.

Just incase anyone has come across my full problem before, I'll go on to describe it (there's no point in reinventing the wheel!), but I'm not looking for someone to write all my code for me, just help with populating the listbox at the moment!

Once the listbox is populated, the user will then be able to multiselect items from it, and this will then extract only rows that contain one of the selected items from the listbox in the specified column to a new sheet

Thanks

Charlie
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
1)
Code:
Private Sub UserForm_Initialize()
Dim a, e
With Sheets("YourShetNameHere") '<- alter to suite
    a = .Range("a1", Range("a" & Rows.Count).Enc(xlUp)).Value
    ' change "a1"/ "a" to appropreate column reference
End With
With CreateObject("Scripting.Dictionary")
    .CompareMode = vbTextCompare
    For Each e In a
        If (Not IsEmpty(e)) * (Not .exists(e)) Then .add e, Nothing
    Next
    Me.ListBox1.List = .keys
End With
End Sub
2) Add CommandButton (CommandButton1)
Code:
Private Sub CommandButton1_Click()
Dim i As Integer, n As Integer, a() As String
With Me.ListBox1
    If .ListIndex = -1 Then Exit Sub
    ReDim a(1 To .ListCount, 1 To 1)
    For i = 0 To .ListCount - 1
        If .Selected(i) Then
            n = n + 1 : b(n,1) = .List(i)
        End IF
    Next
End With
Sheets("YourSheetNameHere").Ragne("YourRangeHere").Resize(n).Value = a '<- fixed
End Sub
 
Upvote 0
This takes the selected items from the List Box, uses them to create a criteria range and activates Advanced Filter to copy the matching rows to the new place.
You will need to ajust the range variables, userform name and listbox name to meet your situation. The fromList and dataRange should be set to include the header row.
Code:
Sub Macro1()
Dim fromList As Range
Dim toPlace As Range
Dim dataRange As Range
Dim selectedCount As Long, i As Long

Set fromList = ThisWorkbook.Sheets(1).Range("A:A")
Set toPlace = ThisWorkbook.Sheets(2).Range("a1")
Set dataRange = fromList.Parent.UsedRange

Set toPlace = toPlace.Range("a1"):Rem adjust toPlace above, not here.
toPlace.Value = fromList.Range("a1").Value
With UserForm1
    For i = 0 To .ListCount - 1
        If .Selected(i) Then
            selectedCount = selectedCount + 1
            toPlace.Offset(selectedCount, 0) = "'=" & .List(i)
        End If
    Next i
End With
dataRange.AdvancedFilter Action:=xlFilterCopy, _
                          CriteriaRange:=toPlace.Resize(selectedCount + 1, 1), _
                          CopyToRange:=toPlace, Unique:=False
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,377
Members
448,955
Latest member
BatCoder

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top