Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
#6
Posted to microsoft.public.excel.programming
|
|||
|
|||
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! |
#7
Posted to microsoft.public.excel.programming
|
|||
|
|||
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! |
#8
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Straight line depreciation | Excel Worksheet Functions | |||
Straight Line is not straight | Excel Discussion (Misc queries) | |||
straight line graph, really straight line.. | Excel Discussion (Misc queries) | |||
best fit straight line | Charts and Charting in Excel | |||
I want a straight answer | Excel Worksheet Functions |