I have this function which returns as string the value in comma separated string which is in order of given integer value.
Private Sub TestGetNthNumber()
The other alternative is to use RegEx/RegExp. Your function will looks like that:
Public Function GetNthNumberAlternative(sMark As String, iOrder As Integer) As String
'regexp declaration
Dim objRegExp As Object
Set objRegExp = CreateObject("vbscript.regexp")
With objRegExp
.Global = True
.Pattern = "\d+"
GetNthNumberAlternative = .Execute(sMark)(iOrder - 1).Value
End With
End Function
And you could call it in this way:
Private Sub TestGetNthNumber()
Debug.Print GetNthNumberAlternative("NUMBERS 5088 AND 5089 OR 5090, 5091", 1)
End Sub
You want to use the Split
function. If you have the same values each time then you can remove the NUMBERS
and the final AND
. Something like this:
Private Sub TestGetNthNumber()
Debug.Print GetNthNumber("NUMBERS 5088, 5089, 5090, 5091", 2)
End Sub
Public Function GetNthNumber(sMark As String, iOrder As Integer) As String
Dim vArray As Variant
sMark = Replace(sMark, "NUMBERS", "")
sMark = Replace(sMark, "AND", "")
vArray = Split(sMark, ",")
GetNthNumber = vArray(iOrder)
End Function