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 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
Public Function compareStrAlph(str1 As String, str2 As String) As Boolean
'return TRUE if str1 is before str2 in alphabetical order or is equal to str2
'return FALSE if str1 is after str2 in alphabetical order
'It is based on the ASCII order
'this function is case insensitive (asc("A")= 65 // asc("Z")=90 asc("a")= 97 ... asc("z")=122)
Dim i1 As Integer, i2 As Integer, i3 As Integer
For i1 = 1 To Application.WorksheetFunction.min(Len(str1), Len(str2))
i2 = Asc(Mid(str1, i1, 1))
i3 = Asc(Mid(str2, i1, 1))
If i2 >= 65 And i2 <= 90 Then
i2 = i2 + 32
End If
If i3 >= 65 And i3 <= 90 Then
i3 = i3 + 32
End If
Select Case i2
Case Is < i3 'character from str1 before character from str2
compareStrAlph = True
i1 = Empty
i2 = Empty
i3 = Empty
Exit Function
Case i3 'same character
If i1 = Application.WorksheetFunction.min(Len(str1), Len(str2)) Then 'last character to be compared
If Len(str1) <= Len(str2) Then
compareStrAlph = True
i1 = Empty
i2 = Empty
i3 = Empty
Exit Function
Else
compareStrAlph = False
i1 = Empty
i2 = Empty
i3 = Empty
Exit Function
End If
End If
Case Is > i3 'character from str1 after character from str2
compareStrAlph = False
i1 = Empty
i2 = Empty
i3 = Empty
Exit Function
End Select
i2 = Empty
i3 = Empty
Next i1
i1 = Empty
'NB: The "= empty" declarations are useless because the variables are declared inside the subroutine so can be used only in the subroutine
End Function |
Partager