ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   How to break the FOR loop? (https://www.excelbanter.com/excel-programming/441469-how-break-loop.html)

Eric

How to break the FOR loop?
 
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

Rick Rothstein

How to break the FOR loop?
 
Believe it or not, the command is...

Exit For

--
Rick (MVP - Excel)



"Eric" wrote in message
...
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



Eric

How to break the FOR loop?
 
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" wrote in message
...
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


.


Rick Rothstein

How to break the FOR loop?
 
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" wrote in message
...
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" wrote in message
...
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


.



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

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com