View Full Version : If...Then in For...Next
ger21 03-04-2005, 07:23 PM Can anyone tell me if there is a way to do this. I'm trying to skip over duplicate entries in an array. I'm using VBA
For x = 1 to 50 (not really using 50)
If coord(x) = coord(x-1) then next x
(do stuff here)
Next x
I get an error "Next without For" in the if...then statement. Is this not legal?
Instead I'm doing this:
For x = 1 to 50 (not really using 50)
If coord(x) = coord(x-1) then goto Skip
(do stuff here)
Skip:
Next x
Ken_Shea 03-04-2005, 08:40 PM You can do it this way, I have never timed them but understand while/wend and do/loop are more effiecient then for /next. I also do not have a legitamate data array to test.
Do While X < 50 And Coord(X) = Coord(X - 1)
' Do STuff here
X = X + 1
Loop
Stop
sendkeys 03-04-2005, 09:04 PM think this would also help :P
For x = 1 to 50
If coord(x) = coord(x-1) then
blah=blah
next x
else
msgbox "next ended"
end if
or if you want to keep going to 50. then
For x = 1 to 50
If coord(x) = coord(x-1) then
msgbox "does equal"
else
msgbox "does not equal"
end if
next x
ger21 03-04-2005, 10:43 PM Ken, I want it to skip the "do stuff here" part if the two values are the same.This is for my AutoCAD G-code export macro. I'm trying to skip duplicate points in a polyline. The array is the polylines point list. There is a lot of code to figure out the correct g-code before I get to the Next x. I want to just increment x and skip all the code. Your example doesn't do that.
Sendkeys, I get an error message when the Next x is in the if..then statement.
Ken_Shea 03-05-2005, 12:40 AM OK,
Do While X < 50
If Coord(X) And Coord(X - 1) Then
' Do STuff here
End If
X = X + 1
Loop
Ken_Shea 03-05-2005, 12:49 AM SendKeys,
It looks like Gerry whimped out on us :D
sendkeys 03-05-2005, 02:09 AM For x = 1 to 50
If coord(x) = coord(x-1) then
'do stuff
'do stuff
end if
next x
works same way as kens but ken is right do while is little faster then "if next" so i would do it that way :)
remember you can also use
if not Coord(X) And Coord(X - 1) Then
too :) (i think hehe) vba isnt my strong point normal vb6 is.
ger21 03-05-2005, 07:35 AM SendKeys,
It looks like Gerry whimped out on us :D
Hey, I had to get up at 5:00, and it was almost midnight. :(
I still don't think your code will work. See below.
Do While X < 50
If Coord(X) And Coord(X - 1) Then
' Do Stuff here (I want to skip this part if the two above are equal)
End If
X = X + 1
Loop
I could just use if..then..else, but I think I would have to redo a bunch of code that always takes me an hour to figure out what it's doing. :)
What I'm going to do instead is just iterate through the array, and eliminate duplicates and make a new array, then use the new array. This makes some other things easier anyway. Thanks for the help, guys.
Ken_Shea 03-05-2005, 09:49 AM OK, one more shot :D
Dim coord(50) As Single
Dim NewCoord(50) As Single
coord(1) = 1.234
coord(2) = 4.675
coord(3) = 3.777
coord(4) = 3.777
coord(5) = 11.024
coord(6) = 10.625
coord(7) = 3.98
coord(8) = 5.55
coord(9) = 5.55
coord(10) = 7.825
x = 1
Do While x < 11
If coord(x) = coord(x - 1) Then GoTo Skip
' Do Stuff here (I want to skip this part if the two above are equal)
d = d + 1
NewCoord(d) = coord(x)
Debug.Print d; NewCoord(d)
Skip:
x = x + 1
Loop
Stop
Result
1) 1.234
2) 4.675
3) 3.777
4) 11.024
5) 10.625
6) 3.98
7) 5.55
8) 7.825
drolley 03-17-2005, 01:29 AM Gerry,
Just looking at the first message in the thread...
The problem is the multiple termination points (next x) for the loop. The language sees the first one and terminates the loop scope. The second "next x" is not tied to an open "for" statement and the translator declares an error.
As you found, your flow using the goto behaves the way you want because there is only one "next x".
However, looking at the message a little closer, you could restate the problem slightly.
Your current approach is to skip the work if the adjacent coordinates are the same. Another way to look at it is: You want to do the work if the adjacent coordinates are different.
So this should do what you want:
For x = 1 to 50 (not really using 50)
If coord(x) <> coord(x-1) then
(do stuff here)
end if
Next x
Dave
ger21 03-17-2005, 06:59 AM As usual, the answer was so simple I couldn't see it. Thanks Dave. But I'm going to end up going the other route, creating new arrays. I need to do that to consolidate a bunch of different subs.
TCARPENTER 03-17-2005, 11:50 AM Hi Gerry,
A little help for you - attached is a VB module you can import into your VBA routine to make your array handling life a little easier!
HTH
Todd
datacop 07-13-2006, 07:44 PM I'm sorry.. I know that this thread is almost 2 years old.. but being a professional software developer, I am compelled to speak up :)
For x = 1 to 50
if not(cord(x) = cord(x-1) then
' Stuff to do if they aren't the same
end if
next
Please, if you need any assistance at all... feel free to contact me..
Datacop - MCSD
corey@12mile 07-19-2006, 02:27 PM Try something like this. It will go through the entire array, but on each pass will look through the previous values of the array for a match. I don't understand how any of the previous code samples would find a match between the 3rd and 10th value for instance. At best the other code examples only check the previous value.
I haven't tested the code, but it should work. Either way you will get the idea of what needs to be done.
cd.
function CycleThroughArray(CoordArray as double)
for X = 1 to UBound(CoordArray) - 1 ' for loop to go through array
if X > 1 then ' if we are on first iteration, there is nothing to match against
matchFlag=false ' Set matchFlag to false
for Y = 1 to X - 1 ' for loop to cycle through previous values
if Y = X then ' we have a previous match
matchFlag=true
end if
next Y
end if
if matchFlag=false then ' no match's, good to go
DoSomeStuff(CoordArray(X))
end if
next X
end function
function DoSomeStuff(Coord as double)
' Run your code here
end function
Can anyone tell me if there is a way to do this. I'm trying to skip over duplicate entries in an array. I'm using VBA
For x = 1 to 50 (not really using 50)
If coord(x) = coord(x-1) then next x
(do stuff here)
Next x
I get an error "Next without For" in the if...then statement. Is this not legal?
Instead I'm doing this:
For x = 1 to 50 (not really using 50)
If coord(x) = coord(x-1) then goto Skip
(do stuff here)
Skip:
Next x
ger21 07-19-2006, 03:22 PM I don't understand how any of the previous code samples would find a match between the 3rd and 10th value for instance. At best the other code examples only check the previous value.
Didn't need to. See post #4. Thanks.
|