[英]Comparing version numbers with more than one decimal point in Excel VBA
I have a Worksheet that stores a software version number in a cell and it uses multiple decimal points.我有一个在单元格中存储软件版本号的工作表,它使用多个小数点。 eg 5.2.16例如 5.2.16
I need to be able to do version number comparisons.我需要能够进行版本号比较。 Is the version number in the cell higher or lower than the version number I'm comparing it to.单元格中的版本号是高于还是低于我与之比较的版本号。
A Double can only store a number with a single decimal point, so that was out. Double 只能存储一个带一个小数点的数字,所以就这样了。
One method I've tried is to remove the decimals with Replace and store the number in a Long variable.我尝试过的一种方法是使用 Replace 删除小数并将数字存储在 Long 变量中。
lngMyNumber = Replace("5.2.16", ".", "")
I can then easily compare as numbers but of course if the version number 5.1 (51) was compared to 4.5.10 (4510), 5.1 would not come out as the higher version number.然后我可以轻松地比较数字,但当然,如果将版本号 5.1 (51) 与 4.5.10 (4510) 进行比较,则 5.1 不会作为更高的版本号出现。
Has anyone got a suggestion for an elegant solution?有没有人对优雅的解决方案提出建议?
I believe this is a more robust approach as it does not assume that the version components are the same length:我相信这是一种更强大的方法,因为它不假设版本组件的长度相同:
Function VersionCheck(currVer, latestVer) As Boolean
Dim currArr() As String, latestArr() As String
currArr = Split(currVer, ".")
latestArr = Split(latestVer, ".")
'If versions are the same return true
If currVer = latestVer Then
VersionCheck = True
Exit Function
End If
'Iterate through the version components
Dim i As Integer
For i = LBound(currArr) To UBound(currArr)
'If the end of the latest cersion is reached, the current version must be up to greater
'meaning it is up to date
If i > UBound(latestArr) Then
VersionCheck = True
Exit Function
End If
'Cast the component to an integer
Dim curr As Integer, latest As Integer
curr = Int(currArr(i))
latest = Int(latestArr(i))
'Check which version component is greater in which case return a result
If curr > latest Then
VersionCheck = True
Exit Function
ElseIf curr < latest Then
VersionCheck = False
Exit Function
End If
'If the version components are equal, iterate to the next component
Next
'If there are remaining components in the latest version, return false
If i < UBound(latestArr) Then
VersionCheck = False
Exit Function
End If
End Function结束函数
This should work.这应该有效。 Pass the replaced version number to the function as well as the desired length, which should be the max string length of the versions.将替换的版本号以及所需的长度传递给函数,该长度应该是版本的最大字符串长度。
Function rightSize(ver, verlen As Integer) As Long
Dim i As Integer
For i = Len(ver) To verlen - 1
ver = ver & "0"
Next
rightSize = Int(ver)
End Function
A simpler workaround that works with version numbers of different lengths.一种更简单的解决方法,适用于不同长度的版本号。
Function VersionCheck(CurrVer, PrevVer) As String
Dim A, B, i, CurrVer_0, PrevVer_0 As Variant
CurrVer_0 = CurrVer
PrevVer_0 = PrevVer
While UBound(Split(CurrVer, ".")) > UBound(Split(PrevVer, "."))
PrevVer = PrevVer & ".0"
Wend
While UBound(Split(CurrVer, ".")) < UBound(Split(PrevVer, "."))
CurrVer = CurrVer & ".0"
Wend
A = Split(CurrVer, ".")
B = Split(PrevVer, ".")
For i = 0 To UBound(A)
If CInt(A(i)) > CInt(B(i)) Then
VersionCheck = CurrVer_0
Exit Function
ElseIf CInt(A(i)) < CInt(B(i)) Then
VersionCheck = PrevVer_0
Exit Function
End If
Next i
VersionCheck = CurrVer_0
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.