Home » Controls » How to remove duplicates from list box (in two ways)
How to remove duplicates from list box (in two ways)
Posted on September 24, 2011
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | 'Controls - How to remove duplicates from list box (in two ways) 'Method 1: 'How to add distinct item in a list box Option Explicit Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, _ ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Private Const LB_FINDSTRINGEXACT = &H1A2 Private Sub Form_Load() Dim ItemToAdd() As String Dim I As Integer ItemToAdd = Split("Item1 Item2 Item3 Item4 Item5 Item1 Item2 Item3 Item4 Item5 Item1 Item2") For I = 0 To UBound(ItemToAdd) If SendMessage(List1.hwnd, LB_FINDSTRINGEXACT, -1, ByVal ItemToAdd(I)) = -1 Then List1.AddItem ItemToAdd(I) End If Next End Sub 'Method 2: Dim I As Long Dim J As Long Dim currItem As String For i = 0 To list1.listcount - 1 currItem = list1.list(i) For j = 0 To list1.listcount - 1 If j <> i Then If list1.list(j) = currItem Then list1.removeitem(j) End If End If Next j Next i |
Enjoy this article?
Filed under: Controls
Leave a comment