Excel VBA版本号来自顺序号

时间:2016-01-12 10:09:53

标签: excel vba excel-vba

所有

我有一个Excel电子表格,可以收集来自各个位置的文件,这些文件的名称末尾有版本号。

例如

Filea_v1.1.xlsm Filea_v1.1.1.xlsm Filea_v9.1.xlsm Filea_v11.1.1.xlsm

我已经删除了文件的详细信息并留下了版本号,但我发现很难编写代码来获取最新版本。使用简单的

  If LatestVersion > LatestVersion_Last
  ' 11.1.1 > 9.1

我得到一个FALSE输出,因为它认为9.1大于11.1.1(因为它保存在一个字符串中)。我无法转换为我知道的数字,因为它包含多个小数位。

有人有任何建议吗?

由于

3 个答案:

答案 0 :(得分:0)

Public Fucntion CompareVersion( strVersion1 as String, strVersion2 as String)

strVersion1 = "Filea_v11.1.1.xlsm"
strVersion2 = "Filea_v9.1.xlsm"

strVersion1 = Replace(strVersion1, "Filea_v", "") 'remove prefix
strVersion1 = Replace(strVersion1, ".xlsm", "") ' remove suffix
strVersion1 = Replace(strVersion1, ".", "") 'remove dots

strVersion2 = Replace(strVersion1, "Filea_v", "") 'remove prefix
strVersion2 = Replace(strVersion1, ".xlsm", "") ' remove suffix
strVersion2 = Replace(strVersion1, ".", "") 'remove dots

Dim strVersionArray1 as String()
Dim strVersionArray2 as String()

strVersionArray1 = Split(strVersion1,".")
strVersionArray2 = Split(strVersion2,".")

Dim i as Integer
For i=LBound(strVersionArray1) To UBound(strVersionArray2)
    If (Cint(strVersionArray1(i))>Cint(strVersionArray2(i))) Then
          'strVerion1 is greater than strVersion2
          GoTo EXIT_FUNC:
    Else If (Cint(strVersionArray1(i))<Cint(strVersionArray2(i))) Then
          'strVerion is greater than strVersion2
          GoTo EXIT_FUNC:
    Else If (Cint(strVersionArray1(i))=Cint(strVersionArray2(i))) Then
          'we need to examine the next segment of the array
    End if
Next i

EXIT_FUNC:     结束功能

答案 1 :(得分:0)

我想你可以尝试这样的事情:

Function FctLatest(ByVal LatestVersion As String, ByVal LatestVersion_Last As String) As Boolean

Dim comparedNew, comparedOld As String

'Loop to remove the versions one by one
Do
    'For the new version
    comparedNew = CutString(comparedNew, LatestVersion)

    'For the previous version
    comparedOld = CutString(comparedOld, LatestVersion_Last)

    'we eliminate the case of equal versions
    If CInt(comparedNew) > CInt(comparedOld) Then
        FctLatest = True
        GoTo endFunction
    ElseIf CInt(comparedNew) < CInt(comparedOld) Then
        FctLatest = False
        GoTo endFunction
    End If

Loop While InStr(LatestVersion_Last, ".") <> 0 Or InStr(LatestVersion, ".") <> 0

'For the new version
comparedNew = CutString(comparedNew, LatestVersion)

'For the previous version
comparedOld = CutString(comparedOld, LatestVersion_Last)

'we eliminate the case of equal versions, and choose the first one input
If CInt(comparedNew) > CInt(comparedOld) Then
    FctLatest = True
ElseIf CInt(comparedNew) < CInt(comparedOld) Then
    FctLatest = False
End If

endFunction:

End Function


Private Function CutString(ByVal ReturnedString, ByRef InputString As String) As String

'For latest version
If InStr(InputString, ".") = 0 Then
    ReturnedString = InputString
Else
    ReturnedString = Left(InputString, InStr(InputString, ".") - 1) 'Adding the first part of the version
    InputString = Right(InputString, Len(InputString) - InStr(InputString, ".")) 'Removing the first part of the version
End If

CutString = ReturnedString

End Function

它会返回一个布尔值,比较您感兴趣的版本。

此致

皮尔。

编辑:只是添加了案例,因为它首先不起作用。

答案 2 :(得分:0)

如果第一个输入字符串的版本较大,则此函数返回1;如果第二个输出字符串的版本较大,则返回-1;如果两者相等则返回0:

Function Compare(a As String, b As String) As Integer
    Dim n As Integer
    Dim aarr() As String
    Dim barr() As String
    Dim av As Integer
    Dim bv As Integer

    aarr = Split(a, ".")
    barr = Split(b, ".")

    n = LBound(aarr)
    Do
        av = -1
        If UBound(aarr) > i - 1 Then av = CInt(aarr(i))
        bv = -1
        If UBound(barr) > i - 1 Then bv = CInt(barr(i))

        If av = -1 And bv = -1 Then
            Compare = 0
            Exit Function
        End If

        If av > bv Then
            Compare = 1
            Exit Function
        End If

        If av < bv Then
            Compare = -1
            Exit Function
        End If

        i = i + 1
    Loop
End Function