Not a Member Yet,
Click here to Register

ID: 275
Viewed: 2687
Added: Jul 26, 2002
Version:
Snippet uploaded by: snippet
Written By: Aaron Young
Demo: Sorry, no demo



User Rated at: 0 Stars
Rate This:

Thank you for your vote. Please wait...

It appears you already voted for this snippet

It appears your vote value was empty

Find Duplicates in ListBox

Highlight all by clicking in box
<!---Declaration--->
'Try this, it works fastest With a Sorted List, but Is still quick either way..
' Aaron Young...


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

Highlight All
<!---Code--->
Private Sub Command1_Click()
Dim iIndex As Long
Dim iMatch As Long
Dim iCopies As Long
Dim iHighest As Long
Dim aCommon() As Long
Dim sString As String
Dim bSkip As Boolean

For iIndex = 0 To List1.ListCount - 1
iCopies = 0
iMatch = -1
bSkip = False
'Skip this one if it's the same as the last Item Checked
If iIndex Then bSkip = (List1.List(iIndex) = List1.List(iIndex - 1))
'Skip this one if there's a previous instance of it in the List
If Not bSkip Then bSkip = (SendMessage(List1.hwnd, LB_FINDSTRINGEXACT, -1, ByVal List1.List(iIndex)) < iIndex)
'While there are other Instances in the List..
While iMatch <> iIndex And Not bSkip
'Increment the No of Copies Found of this Item
iCopies = iCopies + 1
'Find the next Copy..
iMatch = SendMessage(List1.hwnd, LB_FINDSTRINGEXACT, IIf(iMatch < 0, iIndex, iMatch), ByVal List1.List(iIndex))
Wend
'If there were more than 1 Copies
If iCopies > 1 And Not bSkip Then
'If the No. of Copies is Greater or the Same as the Highest so far..
If iCopies >= iHighest Then
If iCopies > iHighest Then
'new Highest Copies
ReDim aCommon(0)
Else
'Another Item with the same highest amount of Copies
ReDim Preserve aCommon(UBound(aCommon) + 1)
End If
'Store this Index
aCommon(UBound(aCommon)) = iIndex
'Remember the Highest No. of Copies
iHighest = iCopies
End If
End If
Next
If iHighest Then
'If Copies were Found..
For iIndex = 0 To UBound(aCommon)
sString = sString & ", " & List1.List(aCommon(iIndex))
Next
MsgBox "Most Repeated Item(s): " & vbCrLf & Mid$(sString, 3) & _
vbCrLf & vbCrLf & "Repeated " & iHighest & " Times.", _
vbInformation + vbOKOnly, "Repeats"
Else
'No Copies Found..
MsgBox "No Items were Repeated", vbInformation + vbOKOnly, "No Repeats"
End If
End Sub
;


No Comments to show

Please completely fill out the form below if you want to review this snippet. All reviews are subject to validation.


Replying to a Comment...


Adding your comment. Please wait...

Thanks for adding your comment!. After further review it will be added.

There was a problem adding your comment. Please try again.

Please complete all the fields in the form before sending.

© 2002 - 2024 snippetlibrary.com All Rights Reserved. Conditions
Do NOT follow this link or you will be banned from the site!