Handling series of comma separated values in VBA

后端 未结 2 1922
醉酒成梦
醉酒成梦 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
    
    0 讨论(0)
  • 2021-01-15 00:29

    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
    
    0 讨论(0)
提交回复
热议问题