我正在尝试按照唯一的字母顺序进行自定义排序,以精确地按以下顺序排序。最好使用vba解决方案。
A, P, R, C, D, E, F, G, H, I, J, K, L, M, O, Q, S, T, U, V, W, X, Y, Z, B, N
谢谢 苏吉
答案 0 :(得分:1)
这很简单:
请参见以下代码:
Option Explicit
Sub CustomOrder()
MsgBox CustomComparer("Z", "B") 'shows 1, so B is greater
End Sub
Function CustomComparer(str1 As String, str2 As String) As Long
Dim orderArray As Variant
orderArray = Array("A", "P", "R", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "O", "Q", "S", "T", "U", "V", "W", "X", "Y", "Z", "B", "N")
Dim isFirstFound As Boolean, isSecondfound As Boolean, i As Long, firstIndex As Long, secondIndex As Long
For i = LBound(orderArray) To UBound(orderArray)
If str1 = orderArray(i) Then
firstIndex = i
isFirstFound = True
End If
If str2 = orderArray(i) Then
secondIndex = i
isSecondfound = True
End If
If isFirstFound And isSecondfound Then Exit For
Next
CustomComparer = secondIndex - firstIndex
End Function