VBA Excel - Problem with a Do Until EOF loop

S

Simon

Hi. I'm a newbie to VBA and I'm having a problem with a Do until EOF
loop. In it's most simple terms the program is trying to read data
from a textfile, process it and then import the results into a range
in an excel worksheet. The problem is that it never processes the last
record. It reads the last record and then exits the loop (without
processing it) because it's the end of the file. For reasons that are
too complicated to explain briefly, I can't just duplicate the
processing part of the code after the loop. I've been having to add an
extra record to the end of the text files in order to fix the problem,
but I think there must be an easier way than that. Does anyone know of
one? TIA. Simon
 
K

Ken Macksey

Hi

Try moving the test to the end something like


Do
[statements]

Loop While Not EOF(1)

See the help for Do.

HTH

Ken
 
P

Philippe Oget

I had the same problem, here is my workaround try this:

The short of it is to open the text file as a textstream:

Set TextFile = g_oFSO.GetFile((FileName))


Set TextStream = TextFile.OpenAsTextStream(ForReading,
TristateUseDefault)

Do While Not TextStream.AtEndOfStream

TextLine = TextStream.ReadLine
' If InStr(1, TextLine, "#") > 0 Then
If Mid(TextLine, 1, 1) = "#" Then
DoSomething
Exit Do
Else
DoSomethingElse
End If

Loop


Hope that helps

Philippe
Ken Macksey said:
Hi

Try moving the test to the end something like


Do
[statements]

Loop While Not EOF(1)

See the help for Do.

HTH

Ken



Simon said:
Hi. I'm a newbie to VBA and I'm having a problem with a Do until EOF
loop. In it's most simple terms the program is trying to read data
from a textfile, process it and then import the results into a range
in an excel worksheet. The problem is that it never processes the last
record. It reads the last record and then exits the loop (without
processing it) because it's the end of the file. For reasons that are
too complicated to explain briefly, I can't just duplicate the
processing part of the code after the loop. I've been having to add an
extra record to the end of the text files in order to fix the problem,
but I think there must be an easier way than that. Does anyone know of
one? TIA. Simon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Formating bolded text exclusively. 0
parse a Text file 2
Input past End Of File 1
help with vba loop 3
Strip records from large textfile with VBA 5
VBA speed up 0
Loop issues 9
Click next until the End of File 9

Top