PC Review


Reply
Thread Tools Rate Thread

How to break the FOR loop?

 
 
Eric
Guest
Posts: n/a
 
      9th Apr 2010
Does anyone have any suggestions on how to exit FOR loop?
Thanks in advance for any suggestions
Eric

For Each myCell In myRng.Cells
' if wkbk.Sheets("Check").Range("O18").Value = 0 then exit FOR loop
Next myCell
 
Reply With Quote
 
 
 
 
Rick Rothstein
Guest
Posts: n/a
 
      9th Apr 2010
Believe it or not, the command is...

Exit For

--
Rick (MVP - Excel)



"Eric" <(E-Mail Removed)> wrote in message
news:B112EB38-DFB2-4CF2-87E4-(E-Mail Removed)...
> Does anyone have any suggestions on how to exit FOR loop?
> Thanks in advance for any suggestions
> Eric
>
> For Each myCell In myRng.Cells
> ' if wkbk.Sheets("Check").Range("O18").Value = 0 then exit FOR loop
> Next myCell


 
Reply With Quote
 
Eric
Guest
Posts: n/a
 
      9th Apr 2010
Does it look like what you suggest?
When I open this file, it pops up a message about having Next without For.
Error stop here as shown below.
Do you have any suggestions on how to fix it?
Thanks in advance for any suggestions
Eric

For Each myCell In myRng.Cells
if wkbk.Sheets("Check").Range("O18").Value = 0 then
Exit For
Next myCell ' Pointing here


"Rick Rothstein" wrote:

> Believe it or not, the command is...
>
> Exit For
>
> --
> Rick (MVP - Excel)
>
>
>
> "Eric" <(E-Mail Removed)> wrote in message
> news:B112EB38-DFB2-4CF2-87E4-(E-Mail Removed)...
> > Does anyone have any suggestions on how to exit FOR loop?
> > Thanks in advance for any suggestions
> > Eric
> >
> > For Each myCell In myRng.Cells
> > ' if wkbk.Sheets("Check").Range("O18").Value = 0 then exit FOR loop
> > Next myCell

>
> .
>

 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      9th Apr 2010
Using Exit For does not relieve you of the responsibility of closing off
your If..Then statement. Try this...

For Each myCell In myRng.Cells
If wkbk.Sheets("Check").Range("O18").Value = 0 Then
Exit For
End If
Next myCell ' Pointing here

However, I would point out that your loop, as shown, doesn't make much sense
as you aren't using myCell anywhere within it... if O18 on your Check sheet
equals 0, your loop will stop before it gets going. I'm guessing there is a
lot code you didn't show us. Since your If..Then statement is not dependent
on your loop, I would do it something like this instead...

If wkbk.Sheets("Check").Range("O18").Value = 0 Then
For Each myCell In myRng.Cells
' Your loop code goes here
Next myCell ' Pointing here
End If

--
Rick (MVP - Excel)



"Eric" <(E-Mail Removed)> wrote in message
news:1EC81D66-2549-4289-B672-(E-Mail Removed)...
> Does it look like what you suggest?
> When I open this file, it pops up a message about having Next without For.
> Error stop here as shown below.
> Do you have any suggestions on how to fix it?
> Thanks in advance for any suggestions
> Eric
>
> For Each myCell In myRng.Cells
> if wkbk.Sheets("Check").Range("O18").Value = 0 then
> Exit For
> Next myCell ' Pointing here
>
>
> "Rick Rothstein" wrote:
>
>> Believe it or not, the command is...
>>
>> Exit For
>>
>> --
>> Rick (MVP - Excel)
>>
>>
>>
>> "Eric" <(E-Mail Removed)> wrote in message
>> news:B112EB38-DFB2-4CF2-87E4-(E-Mail Removed)...
>> > Does anyone have any suggestions on how to exit FOR loop?
>> > Thanks in advance for any suggestions
>> > Eric
>> >
>> > For Each myCell In myRng.Cells
>> > ' if wkbk.Sheets("Check").Range("O18").Value = 0 then exit FOR loop
>> > Next myCell

>>
>> .
>>

 
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
Break out of a loop in Excel2007? Don Microsoft Excel Misc 8 4th May 2009 09:48 PM
Break Code within While Loop =?Utf-8?B?UkZyZWNoZXR0ZQ==?= Microsoft Access VBA Modules 2 26th Oct 2007 06:37 PM
Can not break out of While loop. Mike Langworthy Microsoft C# .NET 2 14th May 2007 05:50 AM
Break fr Loop =?Utf-8?B?U3dpc2g=?= Microsoft Access Macros 1 9th Nov 2005 07:43 AM
Loop for Page Break Bruce Roberson Microsoft Excel Programming 3 16th Mar 2004 08:03 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:52 AM.