簡體   English   中英

在VBA中使用Excel的比較運算符?

[英]Using Excel's comparison operator in VBA?

我試圖弄清楚如何使用與Excel相同的比較運算符來對字母數字值進行排序,如下所示:

0
9
34
51
123abc
15
a
a1b23c
i
z
34ui
10
d
1
12

排序時,結果如下:

0
1
9
10
12
15
34
51
123abc
34ui
a
a1b23c
d
i
z

是否可以使用Excel用於獲得此結果的比較運算符? 還是有必要為此創建我自己的功能?

我只是繼續創建了一個比較函數,該函數的返回值與StrComp()因為似乎還沒有返回值。

Function ExcelCompare(ByVal str1 As String, ByVal str2) As Integer
    Dim isnum1 As Boolean
    Dim isnum2 As Boolean
    isnum1 = IsNumeric(str1)
    isnum2 = IsNumeric(str2)
    ExcelCompare = StrComp(str1, str2)
    If isnum1 And Not isnum2 Then
        ExcelCompare = -1
    ElseIf Not isnum1 And isnum2 Then
        ExcelCompare = 1
    ElseIf isnum1 And isnum2 Then
        Dim num1 As Double
        Dim num2 As Double
        num1 = CDbl(str1)
        num2 = CDbl(str2)
        If num1 = num2 Then
            ExcelCompare = 0
        ElseIf num1 < num2 Then
            ExcelCompare = -1
        Else
            ExcelCompare = 1
        End If
    End If
End Function

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM