Last updated: 12 Jul 2002
You can look for a tab character in a text sring using Chr$(9).
"Paul Bent" <paulbent@northwindit.co.uk> writes:
(The following script) loads parsed values into a passed in array and returns it to the calling proc:
Public Function fSplitLineByTabs(Byval strLine As String, _ astrData() As String) As Integer '--- Given a tab delimited string, parses it into "fields" '--- and loads them in an array '--- Author: Paul Bent, Nothwind IT Systems, 14-Apr-2001 '--- Parameters ' [In] ' strLine: the string from which to parse the field values ' [In/Out] ' astrData: array to load the field values into '--- Return value ' returns the number of field names placed in the array Dim intP1 As Integer 'Position in string Dim intP2 As Integer 'Position in string Dim intP3 As Integer 'Position in string Dim intC1 As Integer 'Array index 'Initialize the parameter passed in by reference Erase astrData 'Check strLine is not null If Len(strLine) = 0 Then Exit Function 'Parse the field names intP1 = 1 Do While Instr(intP1, strLine, Chr$(9)) > 0 'Size the array Redim Preserve astrData(0 To intC1) intC1 = intC1 + 1 'Get the next tab char intP2 = Instr(intP1, strLine, Chr$(9)) If intP2 > 0 Then 'Get the following tab character intP3 = Instr(intP2 + 1, strLine, Chr$(9)) If intP3 > 0 Then 'If there's another tab char then parse the text between them astrData(intC1) = Mid$(strLine, intP2 + 1, intP3 - intP2 - 1) Else 'Last field in the string astrData(intC1) = Right$(strLine(Len(strLine) - intP2) Exit Do End If 'Reposition the starting point in the input string intP1 = intP2 + 1 Loop 'Return value fSplitLineByTabs = intC1 End Function
Then call it from your sub as follows:
Dim intC1 As Integer Dim astrData() As String If fSplitLineByTabs(strToBeSplit, astrData) Then For intC1 = Lbound(astrData) To Ubound(astrData) 'Print the values to the Output pane Print astrData(intC1) Next End If