Changing Do Loop to For loop

V

Varun

Folks,

I need to change the Do loop below to a For loop reason being that Do loop
as structured below grabs the 1st instance of the string whereas I want the
last instance of the string and I think For loop will let me do that but I am
getting an error.

Do Loop (ORIGINAL):

Do While Not objTechAttrFile.AtEndOfStream
strLine = objTechAttrFile.Readline

If InStr(strLine, "tech.tech") <> 0 Then
Buf() = qc_Split(strLine)
TechFileVersion = Buf(3) 'get version of tech file to open
Exit Do
End If

Loop

Issue above is that I can't think i.e. can't figure out how to change to For
loop without getting an error. Here's my for loop to get the last instance
of string tech.tech in the file but I get an error.

For Loop (DOESN'T WORK):

For tech_attribute_file_lines = 1 To objTechAttrFile.AtEndOfStream
strLine = objTechAttrFile.Readline

If InStr(strLine, "tech.tech") <> 0 Then
Buf() = qc_Split(strLine)
TechFileVersion = Buf(3) 'get version of tech file to open
End If

Next tech_attribute_file_lines

What's wrong with the FOR loop? Help is appreciated
 
B

Bernie Deitrick

Varun,

You still want to use a DoWhile loop (since you don't know ahead of time how
big the file it), but just remove the line

Exit Do

and it will find the last instance of what you are looking for.

HTH,
Bernie
MS Excel MVP
 
V

Varun

Thank you, Bernie. That did the trick.

Bernie Deitrick said:
Varun,

You still want to use a DoWhile loop (since you don't know ahead of time how
big the file it), but just remove the line

Exit Do

and it will find the last instance of what you are looking for.

HTH,
Bernie
MS Excel MVP
 

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

Top