View Full Version : New, faster GetPart function without recursion using Split()


Guus2005
07-09-2009, 01:17 AM
This is a faster GetPart function using the Split() command.

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 FunctionEnjoy!