1. ## If...Then in For...Next

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?

For x = 1 to 50 (not really using 50)

If coord(x) = coord(x-1) then goto Skip

(do stuff here)

Skip:

Next x

2. 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

3. 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

4. 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.

5. OK,
Do While X < 50

If Coord(X) And Coord(X - 1) Then

' Do STuff here

End If

X = X + 1

Loop

6. SendKeys,
It looks like Gerry whimped out on us

7. 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.

8. Originally Posted by Ken_Shea
SendKeys,
It looks like Gerry whimped out on us
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.

9. OK, one more shot

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

10. 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

11. 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.

12. 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

Page 1 of 2 12 Last