这是您可能想要查看的文章/帖子。我对您的问题的第一印象是您可能正在尝试进行版本号比较。文本中点分隔符的长度和数量可能非常重要。现在请检查一下,
http://www.dbforums.com/microsoft-excel/1670840-compare-version-numbers-return-highest-value.html
否则你也可以试试log
:
=A1*10^(4-INT(LOG(A1)))
或者对尾随.
点进行替换,第二个文本肯定会变成小数:
例如,1.3.4 将是 1.34,而 1.3.4.1.3 将是 1.3413
1.2.5.6 将是 125.6,1.2.4.6.1 将是 124.61
PS:不是机前。将为您提供我基于点分隔符拆分和比较的另一个代码。
使用函数编辑:这会将两个版本号与任意数量的点进行比较,将其视为字符串/文本。然而,在 1.3.1 和 1.21.1 的情况下,这将 1.21.1 作为最高数字。
Option Explicit
Function versionNumberComparison(ByRef rng1 As Range, ByRef rng2 As Range) As String
Dim i As Integer
Dim arrVersion1 As Variant, arrVersion2 As Variant
Dim strVer1 As String, strVer2 As String
Dim bool2 As Boolean, bool1 As Boolean
Dim x As Long, y As Long
Application.EnableEvents = False
If Not IsEmpty(rng1.Value) Then
strVer1 = rng1.Value
arrVersion1 = Split(rng1.Value, ".")
Else
versionNumberComparison = "Version number empty"
GoTo Zoo
End If
If Not IsEmpty(rng2.Value) Then
strVer2 = rng2.Value
arrVersion2 = Split(rng2.Value, ".")
Else
versionNumberComparison = "Version number empty"
GoTo Zoo
End If
If UBound(arrVersion1) > UBound(arrVersion2) Then
x = UBound(arrVersion1)
y = UBound(arrVersion2)
ElseIf UBound(arrVersion1) < UBound(arrVersion2) Then
x = UBound(arrVersion2)
y = UBound(arrVersion1)
Else
x = UBound(arrVersion1)
y = x
End If
i = 0
While i <= y
If IsNumeric(arrVersion1(i)) And IsNumeric(arrVersion2(i)) Then
If CInt(Trim(arrVersion1(i))) = CInt(Trim(arrVersion2(i))) Then
If i = y Then
If x <> y Then
If Len(strVer1) > Len(strVer2) Then
bool1 = True
bool2 = False
GoTo PrintOut
Else
bool2 = True
bool1 = False
GoTo PrintOut
End If
End If
End If
bool1 = False
bool2 = False
ElseIf CInt(Trim(arrVersion1(i))) > CInt(Trim(arrVersion2(i))) Then
bool1 = True
bool2 = False
GoTo PrintOut
Else
bool2 = True
bool1 = False
GoTo PrintOut
End If
Else
versionNumberComparison = "Enter Valid version numbers"
GoTo Zoo
End If
i = i + 1
Wend
PrintOut:
If bool1 Then
versionNumberComparison = strVer1
ElseIf bool2 Then
versionNumberComparison = strVer2
Else
versionNumberComparison = "Both the same"
End If
Zoo:
Application.EnableEvents = True
End Function
输出: