I have a number of different entries in a listbox (they vary from 1 to 1001, so making a basic (set) IF statement will not work. The entries must not be the same. If they are the same, a messagebox will show saying so.
How can i do this?
Thanks
Dave
Printable View
I have a number of different entries in a listbox (they vary from 1 to 1001, so making a basic (set) IF statement will not work. The entries must not be the same. If they are the same, a messagebox will show saying so.
How can i do this?
Thanks
Dave
To quickly catch duplicates, you can set the Sorted property of the listbox to True. However I still think you will need to run a loop to automatically remove duplicates.
.
Filter duplicates without API :
VB Code:
Dim i As Long, J As Long With .cboPending 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 End With
Irrespective of the number of entries, you are going to have to llop through the contents of both listboxes to find out if there's a match.
Mega.VB Code:
Private Sub Command1_Click() Dim FirstBox As Integer Dim SeconBox As Integer Dim bMatch As Boolean bMatch = False For FirstBox = 0 To List1.ListCount - 1 For SeconBox = 0 To List2.ListCount - 1 If LCase(List1.List(FirstBox)) = LCase(List2.List(SeconBox)) Then bMatch = True Next Next If bMatch Then MsgBox "Item exists" Else MsgBox "No matches in the lists" End If End Sub
GODDAM. Beaten again :)
Actually, forget what I said, I thought you were comparing entries in 2 list boxes.
Never mind, I will leave my code for those who want to do that :)
Mega.
Mega_Man: You don't have to loop through the entries.
VB Code:
Public 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 Public Const CB_FINDSTRING = &H14C ' For a ComboBox Public Const LB_FINDSTRING = &H18F ' For a ListBox Dim lngRetVal As Long ' If the value is in the list then this returns its ListIndex, otherwise it returns -1 lngRetVal = SendMessageString(MyListbox.hwnd, LB_FINDSTRING, -1&, "value to find")
Quote:
Filter duplicates without API :
Now he has both...Quote:
You don't have to loop through the entries.
;)