Моя строка su=45, nita = 30.8, raj = 60, gita = 40.8
.Это имеет отношение к вопросу SO Извлечение максимального числа из строки Я использую функцию maxNums
и получаю результат как 40,8, тогда как я хотел бы, чтобы это было 60. Где поправка в строке кода даст мне желаемоеresult.Code воспроизводится ниже, чтобы избежать перекрестных ссылок. Если эта строка содержит все числа с десятичной точкой, то я получаю правильный результат, но данные из внешних источников могут иметь целые числа.
Option Explicit
Option Base 0 '<~~this is the default but I've included it because it has to be 0
Function maxNums(str As String)
Dim n As Long, nums() As Variant
Static rgx As Object, cmat As Object
'with rgx as static, it only has to be created once; beneficial when filling a long column with this UDF
If rgx Is Nothing Then
Set rgx = CreateObject("VBScript.RegExp")
End If
maxNums = vbNullString
With rgx
.Global = True
.MultiLine = False
.Pattern = "\d*\.\d*"
If .Test(str) Then
Set cmat = .Execute(str)
'resize the nums array to accept the matches
ReDim nums(cmat.Count - 1)
'populate the nums array with the matches
For n = LBound(nums) To UBound(nums)
nums(n) = CDbl(cmat.Item(n))
Next n
'test array
'Debug.Print Join(nums, ", ")
'return the maximum value found
maxNums = Application.Max(nums)
End If
End With
End Function