PC Review


Reply
Thread Tools Rate Thread

Changing Do Loop to For loop

 
 
Varun
Guest
Posts: n/a
 
      26th Mar 2009
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
 
Reply With Quote
 
 
 
 
Bernie Deitrick
Guest
Posts: n/a
 
      27th Mar 2009
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


"Varun" <(E-Mail Removed)> wrote in message
news:88E3C167-410A-4B19-BCE3-(E-Mail Removed)...
> 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


 
Reply With Quote
 
Varun
Guest
Posts: n/a
 
      27th Mar 2009
Thank you, Bernie. That did the trick.

"Bernie Deitrick" wrote:

> 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
>
>
> "Varun" <(E-Mail Removed)> wrote in message
> news:88E3C167-410A-4B19-BCE3-(E-Mail Removed)...
> > 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

>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Simple window application with a loop and a button to eventuallystop the loop Joe Cool Microsoft C# .NET 15 29th Jul 2009 08:40 PM
returning back to loop check condition without completing the loop ashish128 Microsoft Excel Programming 13 3rd Apr 2008 12:53 PM
loop through field names, then use to loop through records topopulate grid pmacdiddie@gmail.com Microsoft Access Form Coding 1 4th Jan 2008 01:13 PM
Advancing outer Loop Based on criteria of inner loop =?Utf-8?B?RXhjZWxNb25rZXk=?= Microsoft Excel Programming 1 15th Aug 2005 05:23 PM
Convert loop with Match function to avoid nested loop??? Kobayashi Microsoft Excel Programming 2 17th Mar 2004 06:36 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:51 AM.