adapted from
http://j-walk.com/ss/excel/tips/tip47.htm
Private Sub ItmDescSearch_Click()
Dim c As Range, f As Range
lbSamDesc.Clear
Dim RangeToSearch As Range
Dim fFirst As Range
Set RangeToSearch = Worksheets("EquipmentData").Range("C3", Worksheets("EquipmentData").Range("C" & Rows.Count).End(xlUp))
Set f = RangeToSearch.Find(ItemDescription.Value, lookat:=xlPart)
Set fFirst = f
Do Until f Is Nothing
lbSamDesc.AddItem f.Value
Set f = RangeToSearch.FindNext(f)
If f.Address = fFirst.Address Then Exit Do
Loop
Dim i As Long, j As Long
Dim nodupes As New Collection
Dim Swap1, Swap2, Item
With lbSamDesc
For i = 0 To .ListCount - 1
' The next statement ignores the error caused
' by attempting to add a duplicate key to the collection.
' The duplicate is not added - which is just what we want!
On Error Resume Next
nodupes.Add .List(i), CStr(.List(i))
Next i
' Resume normal error handling
On Error GoTo 0
'Clear the listbox
.Clear
'Sort the collection (optional)
For i = 1 To nodupes.Count - 1
For j = i + 1 To nodupes.Count
If nodupes(i) > nodupes(j) Then
Swap1 = nodupes(i)
Swap2 = nodupes(j)
nodupes.Add Swap1, before:=j
nodupes.Add Swap2, before:=i
nodupes.Remove i + 1
nodupes.Remove j + 1
End If
Next j
Next i
' Add the sorted and non-duplicated items to the ListBox
For Each Item In nodupes
.AddItem Item
Next Item
End With
End Sub
Bookmarks