如何在Excel公式或VBA中查找文本字符串中的最大数字

时间:2018-04-20 10:26:34

标签: excel vba excel-vba excel-formula

这是我在这里的第一篇文章。我希望从这种类型的文本中获得最大的数字。这是一个例子。

  

第1类 - $ 250,000 - PTD等于本金sumClass 2 - $ 500,000 - PTD等于本金sumClass 3 - $ 500,000 - PTD等于本金sumClass 4 - $ 250,000第5类 - $ 250,000第6类 - $ 250,000

该号码的每个人都会有美元符号。我尝试过斯科特的解决方案here。但没有运气。

请告诉我是否以及如何完成。

谢谢。

4 个答案:

答案 0 :(得分:1)

我会这样:

Function GetMax(s As String)
    Dim val As Variant
    Dim num As Double
    Dim pos As Long

    For Each val In Split(s, "$")
        pos = 0
        Do While IsNumeric(Mid(val, 1, pos + 1))
            pos = pos + 1
        Loop
        If pos > 0 Then
            num = CDbl(Mid(val, 1, pos))
            If num > GetMax Then GetMax = num
        End If
    Next
End Function

答案 1 :(得分:0)

您可以通过首先使用VBAs删除所有“$”符号来调整您链接的答案替换功能:

$txt

根据您的新要求,这里有一个可能的基于正则表达式的解决方案(基于此https://stackoverflow.com/a/44339803/1011724):

Function MaxInString(rng As String) As Double
Dim splt() As String
Dim i&

'==================NEW LINE==================='
rng = Replace(rng, "$", "")
'============================================='

splt = Split(rng)
For i = LBound(splt) To UBound(splt)
    If IsNumeric(splt(i)) Then
        If splt(i) > MaxInString Then
            MaxInString = splt(i)
        End If
    End If
Next i
End Function

请务必首先按照此答案中的第1步https://stackoverflow.com/a/22542835/1011724首先添加对regex库的引用。

答案 2 :(得分:0)

尝试此代码(代码中的必要注释):

Option Explicit
Sub GetMaxNumber()
    Dim txt As String, idx As Long, idx2 As Long, maxValue As Long, extractedNumber As Long, char As String
    maxValue = 0
    'set variable in a code or use cell value
    'txt = Range("A1").Value
    txt = "Class 1 - $250,000 - PTD equal to principal sumClass 2 - $500,000 - PTD equal to principal sumClass 3 - $500,000 - PTD equal to principal sumClass 4 - $250,000 Class 5 - $250,000 Class 6 - $250,000"
    idx = InStr(1, txt, "$")
    'on each loop we will look for dollar sign (you mentioned, that every number starts with it)
    'and then, look for first non-comma non-numeric characted, there the number will end
    'at the end we extract the number from text
    Do While idx > 0
        idx2 = idx + 1
        char = Mid(txt, idx2, 1)
        'determine the end of a number
        Do While IsNumeric(char) Or char = ","
            char = Mid(txt, idx2, 1)
            idx2 = idx2 + 1
        Loop
        'extract the number, also removing comma from it
        extractedNumber = Replace(Mid(txt, idx + 1, idx2 - idx - 2), ",", "")
        'if extracted number is greater than current max, replace it
        If maxValue < extractedNumber Then maxValue = extractedNumber
        idx = InStr(idx + 1, txt, "$")
    Loop

    MsgBox maxValue
End Sub

答案 3 :(得分:0)

我会拆分空格,然后循环查找美元符号,然后找到将字符串粘贴到单元格中以解析逗号等。

请注意,以下代码使用单元格解析器去除逗号和货币符号。

Sub Test2()
    Sheet1.Cells(1, 1).Value = "3,000"
    Debug.Assert Sheet1.Cells(1, 1).Value = 3000

    Sheet1.Cells(1, 1).Value = "$250,000"
    Debug.Assert Sheet1.Cells(1, 1).Value = 250000

End Sub

以下是完整列表

Sub Test()

    Dim s As String
    s = "Class 1 - $250,000 - PTD equal to principal sumClass 2 - $500,000 - PTD equal to principal sumClass 3 - $500,000 - PTD equal to principal sumClass 4 - $250,000 Class 5 - $250,000 Class 6 - $250,000"


    Dim vSplit As Variant
    vSplit = Split(s, " ")

    Dim ccyMax As Currency
    ccyMax = -1

    Dim vSplitLoop As Variant
    For Each vSplitLoop In vSplit

        If Left$(vSplitLoop, 1) = "$" Then
            Sheet1.Cells(1, 1).Value = vSplitLoop
            Dim ccyParsed As Currency
            ccyParsed = Sheet1.Cells(1, 1).Value
            If ccyParsed > ccyMax Then ccyMax = ccyParsed
        End If

    Next

    Debug.Print ccyMax

End Sub