View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Rick Rothstein \(MVP - VB\)[_2251_] Rick Rothstein \(MVP - VB\)[_2251_] is offline
external usenet poster
 
Posts: 1
Default Importing text file without end-of-line delimiter?

In the VB world, the end of line marker is a Carriage Return followed by a
Line Feed... if your lines are not delimited with this, Line Input does not
see your individual lines of text as individual lines. I'm guessing your
lines are delimited by simple Line Feeds. If this is the case, and if this
code will only be used for files generated in the same way as the one you
are currently working with, you could do away with the Do-Loop, read the
entire file into a variable and then Split the text into individual lines
using the Split function with vbLf, or Chr(10) if you prefer, as the
delimiter... this will produce a zero-based array that you can loop through
element by element (which will then be line-by-line) and do whatever you
need to. For example...

Dim X As Long
Dim TotalFile As String
Dim Paragraphs() As String
Line Input #1, TotalFile
Paragraphs = Split(TotalFile, vbLf)
For X = 0 To UBound(Paragraphs)
' Do whatever you need to on each line in your file
Debug.Print Paragraphs(X)
Next

Remember, this code will only work for Line Feed delimited text... if your
code could have to handle both Line Feed delimited text and text delimited
with Windows' normal line delimiter (a Carriage Return followed by a Line
Feed), then write back and I'll generalize the code for you.

Rick


"ker_01" wrote in message
...

In Excel, I recorded a macro while importing a text file (because that's
the easiest way I've found to capture the fixed-width values for my
fields). It imported just fine.

Now I'm using VBA to import that same text file a line at a time and parse
each line for data values. I was having trouble (after the first line, the
code would quit). I used a msgbox to display that first line prior to
parsing- and it showed me the whole file! (or as much as could fit in a
msgbox, anyway).

So somehow, on importing the text file via the menu (file/open), Excel
knew that there were multiple lines and put the data in multiple rows.
However, when I use:

Do While Not EOF(1)
Line Input #1, LineofText
MsgBox LineofText
Loop

It opens the entire file as one line.

Clearly I'm missing something here, but I'm not sure what. Any
suggestions?

Thank you,
Keith