Return Nth item from delimited string


Function StringPartGet(ByVal strList As String, lngPartnr As Long, Delimiter As String, _
                        Optional Compare As VbCompareMethod = vbBinaryCompare) As String
'Return Nth item from delimited string
'Testcases
'?StringPartGet("a,b", 0,",") = ""
'?StringPartGet("a,b", 1,",") = "a"
'?StringPartGet("a,b", 2,",") = "b"
'?StringPartGet("a,b", 3,",") = error 9 subscript out of range

Dim raObjectNames() As String
    If lngPartnr < 1 Then Exit Function
    raObjectNames() = Split(strList, Delimiter, Compare:=Compare)
    If UBound(raObjectNames) > LBound(raObjectNames) Then
        StringPartGet = raObjectNames(LBound(raObjectNames) + lngPartnr - 1)
    End If
End Function