Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 142
Default For-next loop question

I have a for-next loop, within which I want to check to see if a certain
condition is true. If it is true, I want to skip the rest that particular
iteration and move on to the next one in the loop. I tried code that looked
like the following, but it just gave me a "next without for" error:

For x = 1 to 11
If condition = true then
next x
End if
Next x

I want to do this without using a GoTo statement. Any suggestions?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 269
Default For-next loop question

There is no built in "Skip to next" function the only loop command available
is Exit For, which is not what you want.

A Goto is really the best option, but if you really do not want to use one
you can use an additional single loop with an Exit For like this.

For x = 1 To 11
For A = 1 To 1
'Condition Check
If Condition = True Then Exit For
'Do more stuff here if Condition=false
Next A
Next x


--
If this helps, please remember to click yes.


"Luke" wrote:

I have a for-next loop, within which I want to check to see if a certain
condition is true. If it is true, I want to skip the rest that particular
iteration and move on to the next one in the loop. I tried code that looked
like the following, but it just gave me a "next without for" error:

For x = 1 to 11
If condition = true then
next x
End if
Next x

I want to do this without using a GoTo statement. Any suggestions?

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 489
Default For-next loop question

Change the If...Then statement to False. If its, True it will go to the next
x. Hope this helps! If so, let me know, click "YES" below.

For x = 1 to 11
If condition = False then
next x
End if
Next x

--
Cheers,
Ryan


"Luke" wrote:

I have a for-next loop, within which I want to check to see if a certain
condition is true. If it is true, I want to skip the rest that particular
iteration and move on to the next one in the loop. I tried code that looked
like the following, but it just gave me a "next without for" error:

For x = 1 to 11
If condition = true then
next x
End if
Next x

I want to do this without using a GoTo statement. Any suggestions?

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 142
Default For-next loop question

Thanks. I'm not against GoTo's as such, but would rather not use them if
possible. But you're right - it does appear to be the best option in this
case.

"Paul C" wrote:

There is no built in "Skip to next" function the only loop command available
is Exit For, which is not what you want.

A Goto is really the best option, but if you really do not want to use one
you can use an additional single loop with an Exit For like this.

For x = 1 To 11
For A = 1 To 1
'Condition Check
If Condition = True Then Exit For
'Do more stuff here if Condition=false
Next A
Next x


--
If this helps, please remember to click yes.


"Luke" wrote:

I have a for-next loop, within which I want to check to see if a certain
condition is true. If it is true, I want to skip the rest that particular
iteration and move on to the next one in the loop. I tried code that looked
like the following, but it just gave me a "next without for" error:

For x = 1 to 11
If condition = true then
next x
End if
Next x

I want to do this without using a GoTo statement. Any suggestions?

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 489
Default For-next loop question

Correction.

For x = 1 to 11
If condition = False then
' do something
End if
Next x

--
Cheers,
Ryan


"Luke" wrote:

I have a for-next loop, within which I want to check to see if a certain
condition is true. If it is true, I want to skip the rest that particular
iteration and move on to the next one in the loop. I tried code that looked
like the following, but it just gave me a "next without for" error:

For x = 1 to 11
If condition = true then
next x
End if
Next x

I want to do this without using a GoTo statement. Any suggestions?



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 142
Default For-next loop question

There's a thought. I'm learning that it is sometimes easier to accomplish
what you want if you test for the opposite of what you want. Thanks.

"Ryan H" wrote:

Correction.

For x = 1 to 11
If condition = False then
' do something
End if
Next x

--
Cheers,
Ryan


"Luke" wrote:

I have a for-next loop, within which I want to check to see if a certain
condition is true. If it is true, I want to skip the rest that particular
iteration and move on to the next one in the loop. I tried code that looked
like the following, but it just gave me a "next without for" error:

For x = 1 to 11
If condition = true then
next x
End if
Next x

I want to do this without using a GoTo statement. Any suggestions?

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 65
Default For-next loop question

On Feb 19, 3:45*pm, Luke wrote:
There's a thought. *I'm learning that it is sometimes easier to accomplish
what you want if you test for the opposite of what you want. *Thanks.



"Ryan H" wrote:
Correction.


For x = 1 to 11
* * *If condition = False then
* * * * * ' do something
* * *End if
Next x


--
Cheers,
Ryan


"Luke" wrote:


I have a for-next loop, within which I want to check to see if a certain
condition is true. *If it is true, I want to skip the rest that particular
iteration and move on to the next one in the loop. *I tried code that looked
like the following, but it just gave me a "next without for" error:


For x = 1 to 11
* * *If condition = true then
* * * * * next x
* * *End if
Next x


I want to do this without using a GoTo statement. *Any suggestions?


How about checking condition before the for .. next?

Do While condition = True
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
Help Loop question maju Excel Programming 1 May 20th 08 03:50 PM
Loop or If then Question. Looping through Excel Programming 5 January 2nd 08 10:45 PM
Loop question aelewis Excel Discussion (Misc queries) 2 October 24th 07 08:12 PM
Loop question Rob Excel Programming 10 September 13th 05 10:50 PM
loop question dabith Excel Programming 6 June 13th 04 05:28 PM


All times are GMT +1. The time now is 11:47 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"