Use the SearchArray function do to the search
The array to search in must be sorted when you call this function, if it's not, use the code in this thread to sort first...
http://vbforums.com/showthread.php?s=&threadid=231925
VB Code:
Public Function SearchArray(strArr() As String, StrToSearch As String, Optional ByVal Compare As VbCompareMethod = vbBinaryCompare) As Long ' ' Made by Michael Ciurescu (CVMichael from vbforums.com) ' Original thread: [url]http://www.vbforums.com/showthread.php?t=231934[/url] ' Dim FComp As Long, LComp As Long FComp = StrComp(StrToSearch, strArr(LBound(strArr)), Compare) LComp = StrComp(StrToSearch, strArr(UBound(strArr)), Compare) If FComp = -1 Then SearchArray = LBound(strArr) - 1 ' less than first ElseIf FComp = 0 Then SearchArray = LBound(strArr) ' equal to first ElseIf LComp = 1 Then SearchArray = UBound(strArr) + 1 ' larger than last ElseIf LComp = 0 Then SearchArray = UBound(strArr) ' equal to last Else ' in between first and last SearchArray = ArrBinarySearch(strArr, StrToSearch, LBound(strArr), UBound(strArr), Compare) End If End Function Private Function ArrBinarySearch(strArr() As String, StrToSearch As String, ByVal First As Long, ByVal Last As Long, Optional ByVal Compare As VbCompareMethod) As Long ' ' Made by Michael Ciurescu (CVMichael from vbforums.com) ' Original thread: [url]http://www.vbforums.com/showthread.php?t=231934[/url] ' Dim Mid As Long, StrC As Long If Abs(Last - First) <= 1 Then ArrBinarySearch = First Else Mid = (First + Last) \ 2 StrC = StrComp(StrToSearch, strArr(Mid), Compare) Select Case StrC Case -1 ArrBinarySearch = ArrBinarySearch(strArr, StrToSearch, First, Mid) Case 0 ArrBinarySearch = Mid Case 1 ArrBinarySearch = ArrBinarySearch(strArr, StrToSearch, Mid, Last) End Select End If End Function




Reply With Quote