获取和比较字符串中的整数

Obtaining and Comparing Integers in a String

我正在尝试输入带有数字的字符串,例如。 (1, 5, 7, 10, 16, 53, 2) 并分析它以查看是否有任何一个值 >= 某个整数值。

我知道有一种 parseInt 函数,但我认为这不适用于字符串的格式和内容,因为它包含多个用逗号分隔的数字。
我知道还有一个函数 (InStr) 可以在字符串中查找某个值,但这不符合我的需要,因为它需要编写从 1 到 1000+ 的案例。

将字符串转换为整数数组可行吗? VBA的过程复杂吗?

伪代码

Dim element As Range
Dim OrgListcolE As Range

For each element in OrgListcolE
    If there exists a value in the element.Value >=16 Then
        ...
    ElseIf there exists a value in element.Value >=51
        ...
    ElseIf there exists a value in element.Value >=251
        ...
    ElseIf there exists a value in element.Value >=1001
        ...
    End If

循环检查:

Sub topcheck()
    Dim s As String, limt As Long
    s = "1, 5, 7, 10, 16, 53, 2"
    arr = Split(s, ", ")
    limt = 50
    For Each a In arr
        If CLng(a) > limt Then
            MsgBox "a value in the string exceeds " & limt
            Exit Sub
        End If
    Next a
    MsgBox "no value in the array exceeds " & limt
End Sub

带有 Evaluate() 的选项:

Sub test1()
    numbers = "1, 5, 7, 10, 16, 53, 2"
    max_num = Evaluate("Max(" & numbers & ")")
    Debug.Print IIf(max_num > 20, "", "no ") & "any number exceed limit"
End Sub

FilterXML()接近

FilterXML() 函数,自 vers. 2013+,允许从 (string) 列表中 return 过滤数字,该列表已转换为格式良好的 xml 内容。

XML content 和个人 limits 作为参数传递给帮助函数 chk,它分配所有发现作为函数结果的“平面”一维数组。 FilterXML() 根据给定的限制在 xml 内容上应用所谓的 XPath 表达式,从而通过 "//s[.>=" & limit & "]".[=22= 形成一个字符串]

调用示例[​​=48=]

Sub TestNumList()
'1) definitions
    Dim myString As String
    myString = "1,5,7,10,16,53,2"   ' comma separated list (here without spaces!)

    Dim xml As String               ' build xml content string
    xml = "<t><s>" & Replace(myString, ",", "</s><s>") & "</s></t>"

    Dim limits()                    ' define limits
    limits = Array(16, 51, 251, 1001)

'2) check results
    Dim i As Long, elems As Variant
    For i = LBound(limits) To UBound(limits)
        '~~~~~~~~~~~~~~~~~~~~~~~~~~
         elems = chk(xml, limits(i))              ' << help function chk
        '~~~~~~~~~~~~~~~~~~~~~~~~~~
        Debug.Print "Limit " & limits(i) & ":", UBound(elems) & " elem(s) found.", Join(elems, ",")
    Next

End Sub

在VB编辑器中显示 window:

    Limit 16:     2 elem(s) found.            16,53
    Limit 51:     1 elem(s) found.            53
    Limit 251:    0 elem(s) found.            
    Limit 1001:   0 elem(s) found.   

帮助功能chk()

Function chk(xml As String, ByVal limit As Long)
'Purpose: assign listed numbers >= individual limit to 1-dim array
'Author:  https://whosebug.com/users/6460297/t-m
'Date:    2021-07-15
'a) filter elements greater or equal limit
    Dim tmp
    tmp = Application.FilterXML(xml, "//s[.>=" & limit & "]")
'b) return elements as 1-based "flat" 1-dim array
    If TypeName(tmp) = "Variant()" Then  ' found several elements
        chk = Application.Transpose(tmp) ' (change 1-based 2-dim to 1-dim array)
    ElseIf IsError(tmp) Then             ' found no elements at all
        chk = Array(Empty)               ' (empty)
    Else                                 ' found single element only
        ReDim tmp2(1 To 1)               ' provide for one element
        tmp2(1) = tmp                    ' (numeric value)
        chk = tmp2
    End If
End Function

推荐link给@JvdV 近乎百科全书的post治疗FilterXML()