New, faster GetPart function without recursion using Split() (1 Viewer)

Guus2005

AWF VIP
Local time
Today, 12:18
Joined
Jun 26, 2007
Messages
2,645
This is a faster GetPart function using the Split() command.

Code:
Public Function GetPart(strString As String, strSep As String, intPart As Integer) As String
'Guus2005 - 2006/2007
'New GetPart function without recursion, returns part "intPart" of "strString" with "strSep" as a separator
'Uses an array and the Split() function.
    Dim arr() As String
    Dim intHigh As Integer

    On Error GoTo Err_GetPart

    arr = Split(strString, strSep)
    'get number of items in the array
    intHigh = UBound(arr) + 1
    
    If intHigh = 0 Then 'No items in array
        GetPart = strString
        Exit Function
    End If
    
    If intPart = 0 Then
        GetPart = strString
        Exit Function
    ElseIf intPart > intHigh Then
        intPart = intHigh  'Get the last item
    End If
    
    GetPart = Trim(arr(intPart - 1))

Exit_GetPart:
    Exit Function

Err_GetPart:
    MsgBox Err & " - " & Error$, vbExclamation, "Getpart function"
    Resume Exit_GetPart
Resume
End Function
Enjoy!
 

Guus2005

AWF VIP
Local time
Today, 12:18
Joined
Jun 26, 2007
Messages
2,645
Or slightly simpler without all the checks
Code:
Public Function GetPart2(strString As String, strSep As String, intPart As Integer) As String
'Guus2005 - 2006/2012
'Getpart with no checks. If you are sure the parameters are ok.

    GetPart2 = Split(strString, strSep)(intPart - 1)

End Function
Share & Enjoy!
 

Guus2005

AWF VIP
Local time
Today, 12:18
Joined
Jun 26, 2007
Messages
2,645
When you are sure all parameters are okay, you can use this version.

Code:
Public Function GetPart2(strString As String, strSep As String, intPart As Integer) As String
'Guus2005 - 2006/2012
'Getpart with no checks. If you are sure the parameters are ok.

    GetPart2 = Split(strString, strSep)(intPart - 1)

End Function
Share & Enjoy!
 

Users who are viewing this thread

Top Bottom