ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Go straight to next value in a For... Next loop (https://www.excelbanter.com/excel-programming/383778-go-straight-next-value-next-loop.html)

IanKR

Go straight to next value in a For... Next loop
 
Is there a way of going straight to the next value in a For / Next loop,
having tested the current value? I need to do something like this (for
example):

Dim c as Range
For each c in Selection.Cells
If c.Value < 100 Then
Goto Next c '<<<<< I know this is illegal, but it illustrates
what I want to do!
Else
{statements where c.Value = 100}
...
...
End If
Next c

I don't want to Exit For, I just want it to effectively ignore the current
instance of c and move onto the next. I've tried "Next c" but it generates a
compile error.

Thanks in advance.

Ian



Bob Phillips

Go straight to next value in a For... Next loop
 
What you have (without the goto of course) does just that, because the fact
that c.value < 100 means that all of the code in the Else portion does not
get executed, so the next statement is EndIf, which drops into the Next.

--
---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)



"IanKR" wrote in message
...
Is there a way of going straight to the next value in a For / Next loop,
having tested the current value? I need to do something like this (for
example):

Dim c as Range
For each c in Selection.Cells
If c.Value < 100 Then
Goto Next c '<<<<< I know this is illegal, but it illustrates
what I want to do!
Else
{statements where c.Value = 100}
...
...
End If
Next c

I don't want to Exit For, I just want it to effectively ignore the current
instance of c and move onto the next. I've tried "Next c" but it generates
a compile error.

Thanks in advance.

Ian




Jim Jackson

Go straight to next value in a For... Next loop
 
I would construct it this way.

Dim c as Range
For each c in Selection.Cells
If c.Value 100 Then
Do the job
end if
Next c

This way is c is less than 100, it will jump to the "Next c" line and proceed.
--
Best wishes,

Jim


"IanKR" wrote:

Is there a way of going straight to the next value in a For / Next loop,
having tested the current value? I need to do something like this (for
example):

Dim c as Range
For each c in Selection.Cells
If c.Value < 100 Then
Goto Next c '<<<<< I know this is illegal, but it illustrates
what I want to do!
Else
{statements where c.Value = 100}
...
...
End If
Next c

I don't want to Exit For, I just want it to effectively ignore the current
instance of c and move onto the next. I've tried "Next c" but it generates a
compile error.

Thanks in advance.

Ian




Gary''s Student

Go straight to next value in a For... Next loop
 
check out this structu

Sub demo()
For Each c In Selection
If c.Value < 100 Then
Else
msbgox ("time to do something")
End If
Next
End Sub


if the value is less than 100 we do absolutely nothing exept move on to the
next c
--
Gary''s Student
gsnu200707


"IanKR" wrote:

Is there a way of going straight to the next value in a For / Next loop,
having tested the current value? I need to do something like this (for
example):

Dim c as Range
For each c in Selection.Cells
If c.Value < 100 Then
Goto Next c '<<<<< I know this is illegal, but it illustrates
what I want to do!
Else
{statements where c.Value = 100}
...
...
End If
Next c

I don't want to Exit For, I just want it to effectively ignore the current
instance of c and move onto the next. I've tried "Next c" but it generates a
compile error.

Thanks in advance.

Ian




Vergel Adriano

Go straight to next value in a For... Next loop
 
Dim c as Range
For each c in Selection.Cells
If c.Value < 100 Then
'Do Nothing
Else
{statements where c.Value = 100}
...
...
End If
Next c


"IanKR" wrote:

Is there a way of going straight to the next value in a For / Next loop,
having tested the current value? I need to do something like this (for
example):

Dim c as Range
For each c in Selection.Cells
If c.Value < 100 Then
Goto Next c '<<<<< I know this is illegal, but it illustrates
what I want to do!
Else
{statements where c.Value = 100}
...
...
End If
Next c

I don't want to Exit For, I just want it to effectively ignore the current
instance of c and move onto the next. I've tried "Next c" but it generates a
compile error.

Thanks in advance.

Ian




IanKR

Go straight to next value in a For... Next loop
 
Is there a way of going straight to the next value in a For / Next
loop, having tested the current value? I need to do something like
this (for example):

Dim c as Range
For each c in Selection.Cells
If c.Value < 100 Then
Goto Next c '<<<<< I know this is illegal, but it
illustrates what I want to do!
Else
{statements where c.Value = 100}
...
...
End If
Next c

I don't want to Exit For, I just want it to effectively ignore the
current instance of c and move onto the next. I've tried "Next c" but
it generates a compile error.

Thanks in advance.

Ian


Many thanks for all your replies. I feel pretty stupid now - such a simple
solution!



Jim Jackson

Go straight to next value in a For... Next loop
 
Nothing to feel stupid about. Sometimes the simplest fixes are the hardest
to see. We expect everything to be complicated and overlook the simple
things.

Welcome to the club!
--
Best wishes,

Jim


"IanKR" wrote:

Is there a way of going straight to the next value in a For / Next
loop, having tested the current value? I need to do something like
this (for example):

Dim c as Range
For each c in Selection.Cells
If c.Value < 100 Then
Goto Next c '<<<<< I know this is illegal, but it
illustrates what I want to do!
Else
{statements where c.Value = 100}
...
...
End If
Next c

I don't want to Exit For, I just want it to effectively ignore the
current instance of c and move onto the next. I've tried "Next c" but
it generates a compile error.

Thanks in advance.

Ian


Many thanks for all your replies. I feel pretty stupid now - such a simple
solution!




IanKR

Go straight to next value in a For... Next loop
 
Thanks, Jim.

I've been using VBA for about 4 years, now, and have recently got to the
point where I can (usually) write code fairly quickly to do what I want. OK,
it may not be the most elegant or the quickest, but it works! But
occasionally something really simple like this completely stumps me! As you
say, very often it's the simplest solution that works. Sometimes we just
can't see the wood for the trees.

Nothing to feel stupid about. Sometimes the simplest fixes are the
hardest
to see. We expect everything to be complicated and overlook the simple
things.

Welcome to the club!
--
Best wishes,

Jim





All times are GMT +1. The time now is 01:44 AM.

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