FilterXML()
approach
The FilterXML()
function, available since vers. 2013+, allows to return filtered numbers from a (string) list which has been transformed to well-formed xml content.
XML content and individual limits are passed as arguments to help function chk
which assigns all findings to a "flat" 1-dim array as function result. FilterXML()
applies a so called XPath expression on the xml content based on the given limits, thus forming a string via "//s[.>=" & limit & "]"
.
Example call
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
Display in VB Editor's immediate 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.
Help function chk()
Function chk(xml As String, ByVal limit As Long)
'Purpose: assign listed numbers >= individual limit to 1-dim array
'Author: https://stackoverflow.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
Recommended link to @JvdV 's nearly encyclopaedic post treating FilterXML()