Handling series of comma separated values in VBA

后端 未结 2 2008
醉酒成梦
醉酒成梦 2021-01-15 00:10

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()
            


        
2条回答
  •  余生分开走
    2021-01-15 00:21

    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
    

提交回复
热议问题