简体   繁体   中英

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

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.

One method I've tried is to remove the decimals with Replace and store the number in a Long variable.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM