I wrote this ages ago, I think it works fine:
VB Code:
Function SplitText(ByVal Text As String, _ Optional ByVal Delimeter As String = ";", _ Optional ByVal Limit As Long = -1, _ Optional ByVal Compare As VbCompareMethod = vbTextCompare) As String() On Local Error Resume Next 'Add the delimiter to the end of the text so that we return all strings Text = Text + Delimeter 'The Ubound of the array Dim Count As Integer Count = 0 'Find the start/end of the first string Dim StartPos As Long, EndPos As Single StartPos = 1 EndPos = InStr(1, Text, Delimeter, Compare) 'What gets returned Dim ReturnValue() As String 'Loop for all strings Do While EndPos > 0 And (Limit <= -1 Or EndPos <= Limit) 'Add the current string ReDim Preserve ReturnValue(Count) ReturnValue(Count) = Mid(Text, StartPos, EndPos - StartPos) 'Increment the length of the array Count = Count + 1 'Find the next string StartPos = EndPos + Len(Delimeter) EndPos = InStr(StartPos, Text, Delimeter, Compare) Loop 'Return the array SplitText = ReturnValue End Function
Try testing it with something like this:
VB Code:
Private Sub Command1_Click() Dim Texts() As String Texts = SplitText("String1;String2;String3;String4") Dim LoopCounter As Integer For LoopCounter = LBound(Texts) To UBound(Texts) MsgBox Texts(LoopCounter) Next LoopCounter End Sub




Reply With Quote