Function splitString(strToSplit As String, strSplitOn As String) As String()
Dim lp1 As Long, lp2 As Long Dim iLengthStrToSplit As Long Dim iLengthStrSplitOn As Long Dim lCount As Long Dim strArray() As String 'get the length of each of the strings iLengthStrToSplit = Len(strToSplit) iLengthStrSplitOn = Len(strSplitOn) lp1 = 1: lp2 = 1 Do 'find an occorance of the string 'starting from lp1 lp2 = InStr(lp1, strToSplit, strSplitOn) If lp2 = 0 Then lp2 = iLengthStrToSplit + 1 End If 'we only redim every 5000 times the loop ' goes around. 'this makes the code much quicker If lCount Mod 5000 = 0 Then ReDim Preserve strArray(lCount + 5000) End If 'get the bit of the string strArray(lCount) = Mid$(strToSplit, lp1, lp2 - lp1) lp1 = lp2 + iLengthStrSplitOn lCount = lCount + 1 Loop Until lp2 >= iLengthStrToSplit 'redim the array to the exact size ReDim Preserve strArray(lCount - 1) As String splitString = strArray End Function Inputs: You need to pass the string to be split, and what to split the string on. Returns: A zero based array, containgin all of the split strings |