Hi,
Is there any way to remove multiple items for a list. For example, the list:
A
H
A
A
K
Is there any way I could just turn it into:
A
H
K
...and remove the extra A's. Thanks.
Printable View
Hi,
Is there any way to remove multiple items for a list. For example, the list:
A
H
A
A
K
Is there any way I could just turn it into:
A
H
K
...and remove the extra A's. Thanks.
Is this in a listbox? If so you can easily prevent duplicates from occurring in the first place. Do a search for SendMessage. If you still need help, let me know.
Here's a quick and dirty function to remove duplicates from a listbox (I grabbed it from someone on this forum a while back, my apologies, I can't remember who).
VB Code:
' ideally, put this in a module (but it can go directly in your form) Public Sub RemoveDups(ListName As ListBox) 'REMOVE DUPLICATES FROM LISTBOX Dim i As Integer Dim j As Integer With ListName For i = 0 To .ListCount - 1 For j = .ListCount To (i + 1) Step -1 If .List(j) = .List(i) Then .RemoveItem j End If Next Next End With End Sub
Usage:
VB Code:
RemoveDups List1
i like this one, its really fast.
VB Code:
Private Declare Function SendMessageString Lib "user32" Alias "SendMessageA" _ (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long Const LB_FINDSTRINGEXACT = &H1A2 Dim i As Integer For i = List1.ListCount - 1 To 0 Step -1 If SendMessageString(List1.hwnd, LB_FINDSTRINGEXACT, -1, ByVal List1.List(i)) <> i Then List1.RemoveItem i End If Next i
casey.
How would I change that code to make it remove every instance of the dupe? For example:
A
A
B
C
Should be changed to:
B
C
Thanks.
Once again it's easier to prevent the duplicates in the first place.