Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 24
Default For Next loop problem

Hi
Is there a limited time a for next loop will run? (not
counted). The end of the data is marked by text "END".
This works fine for small amounts of data, but seems to
exit around 96 samples. If I then re-run it its fine and
exits correctly.
Thanks
Jo

On Error GoTo errhandler
Dim endofdata
For Each cell In Range("results")
Range("O7").Select
endofdata = ActiveCell
If endofdata = "END" Then GoTo errhandler
various stuff here (cells get deleted and moved up)
Next
errhandler:
Exit Sub


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 97
Default For Next loop problem

"Jo" wrote...
Is there a limited time a for next loop will run? (not
counted). The end of the data is marked by text "END".
This works fine for small amounts of data, but seems to
exit around 96 samples. If I then re-run it its fine and
exits correctly.

...
various stuff here (cells get deleted and moved up)
Next

...

If you're deleting cells, better to iterate from bottom to top or right to left.
So don't use a For Each loop, use

Dim n As Long
':
For n = Range("Results").Rows.Count To 1 Step -1
':

--
To top-post is human, to bottom-post and snip is sublime.
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default For Next loop problem

Jo,

There certainly is no limit in that way. What determines it is the size of
the range that you are iterating through. I would suggest though that
bombing out of the loop the way that you do is not a good practice.

It is probably caused by deleting stuff as you go through, as this can play
havoc with the pointers. It could then work a second time as the first pass
has cleared the path so to speak.

In this instance I would suggest an approach like this


On Error GoTo errhandler
Dim cell As Range
Dim rng As Range
For Each cell In Range("results")
If cell.Value = "END" Then
Exit For
Else
If cell.Value = "a value to be deleted"
If rng Is Nothin g Then
Set rng = cell
Else
Set rng = Union(rng, cell)
End If
End If
Next
If Not rng Is Nothing Then
rng.Entirerow.Delete
End If

errhandler:
Exit Sub

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Jo" wrote in message
...
Hi
Is there a limited time a for next loop will run? (not
counted). The end of the data is marked by text "END".
This works fine for small amounts of data, but seems to
exit around 96 samples. If I then re-run it its fine and
exits correctly.
Thanks
Jo

On Error GoTo errhandler
Dim endofdata
For Each cell In Range("results")
Range("O7").Select
endofdata = ActiveCell
If endofdata = "END" Then GoTo errhandler
various stuff here (cells get deleted and moved up)
Next
errhandler:
Exit Sub




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
do while loop problem April Excel Discussion (Misc queries) 4 October 18th 09 07:51 PM
Loop Problem Todd Huttenstine Excel Programming 10 April 12th 04 06:15 PM
Problem with Loop Mike Excel Programming 7 February 26th 04 09:12 PM
For..Next loop problem Dwaine Horton Excel Programming 1 February 18th 04 12:12 AM
loop problem joao Excel Programming 4 November 6th 03 02:01 PM


All times are GMT +1. The time now is 12:18 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"