Re: Parsing a mostly fixed width text file
- From: "mike williams" <mike@xxxxxxxxxxxxxxxxx>
- Date: Wed, 7 Dec 2005 19:56:20 -0000
"News Reader" <newsreader@xxxxxxxxxxxxxxx> wrote in message
news:uQGHU%23z%23FHA.2628@xxxxxxxxxxxxxxxxxxxxxxx
> Hello Mike, The delimiter is a space, the problem is that will
> work to create the first 3 columns. After that, it's a memo field
> that tends to get broken apart because of the spaces.
If none of the firsdt three fields can themselves contain spaces (and if
each "line" ends with a standard vbCrLf pair) then you can use the Split
function that Rick has already posted to split your lines into their
individual fields. Just in case you're not sure how to blend Rick's Split
function solution into some standard VB file handling code then here is an
example. Paste the code into a VB Form containing a command button and run
the project and click the button. If the fields do not print out as expected
then your data is not quite how you have described it (or the "end of line"
markers are not standard). If that's the case then post again with more
details. (Code below):
Mike
Option Explicit
Private Sub Command1_Click()
Dim n As Long, p As Long, fn As Long
Dim s1 As String, s2() As String, s3() As String
ReDim s3(0 To 3, 0 To 500)
fn = FreeFile
Open "c:\testdata.txt" For Input As fn
While Not EOF(fn)
Line Input #fn, s1
s2() = Split(s1, " ", 4, vbBinaryCompare)
For n = 0 To UBound(s2)
s3(n, p) = s2(n)
Next n
p = p + 1
If p > UBound(s3, 2) Then
ReDim Preserve s3(0 To 3, 0 To UBound(s3, 2) + 500)
End If
Wend
Close fn
ReDim Preserve s3(0 To 3, 0 To p - 1)
' The number of the first record is zero (0)
' UBound(s3, 2) tells you the number of the last record
' print out the first 5 records for test purposes
Me.AutoRedraw = True ' just to make the printing persistent
For n = 0 To 4
For p = 0 To 3
Print s3(p, n)
Next p
Next n
End Sub
.
- References:
- Parsing a mostly fixed width text file
- From: News Reader
- Re: Parsing a mostly fixed width text file
- From: mike williams
- Re: Parsing a mostly fixed width text file
- From: News Reader
- Parsing a mostly fixed width text file
- Prev by Date: Re: Where's the list of still-to-be-written software?
- Next by Date: Re: Where's the list of still-to-be-written software?
- Previous by thread: Re: Parsing a mostly fixed width text file
- Next by thread: Re: Parsing a mostly fixed width text file
- Index(es):
Loading