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 

我得到一个假的输出,因为它认为9.1是大于11.1.1(因为它保存在一个string中)。 我不能转换为我知道的数字,因为它包含多个小数位。

有没有人有什么build议?

谢谢

如果第一个inputstring的版本较大,则此函数返回1;如果第二个inputstring的版本较大,则返回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 
 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:结束函数

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

 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 

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

问候,

皮埃尔。

编辑:刚刚添加的案例,因为它是不是function的第一位。