Page 1 of 2 12 LastLast
Results 1 to 12 of 15

Thread: If...Then in For...Next

  1. #1
    Community Moderator ger21's Avatar
    Join Date
    Mar 2003
    Location
    Shelby Twp, MI....USA
    Posts
    22,289
    Downloads
    0
    Uploads
    0

    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?
    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
    Gerry

    Mach3 2010 Screenset
    http://home.comcast.net/~cncwoodworker/2010.html

    (Note: The opinions expressed in this post are my own and are not necessarily those of CNCzone and its management)


  2. #2
    Gold Member
    Join Date
    Apr 2003
    Location
    Ohio, USA
    Posts
    1,744
    Downloads
    1
    Uploads
    0
    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. #3
    Registered
    Join Date
    Apr 2004
    Location
    usa
    Posts
    439
    Downloads
    0
    Uploads
    0
    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
    Last edited by sendkeys; 03-04-2005 at 09:14 PM.


  4. #4
    Community Moderator ger21's Avatar
    Join Date
    Mar 2003
    Location
    Shelby Twp, MI....USA
    Posts
    22,289
    Downloads
    0
    Uploads
    0
    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.
    Gerry

    Mach3 2010 Screenset
    http://home.comcast.net/~cncwoodworker/2010.html

    (Note: The opinions expressed in this post are my own and are not necessarily those of CNCzone and its management)


  • #5
    Gold Member
    Join Date
    Apr 2003
    Location
    Ohio, USA
    Posts
    1,744
    Downloads
    1
    Uploads
    0
    OK,
    Do While X < 50

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

    ' Do STuff here

    End If

    X = X + 1

    Loop


  • #6
    Gold Member
    Join Date
    Apr 2003
    Location
    Ohio, USA
    Posts
    1,744
    Downloads
    1
    Uploads
    0
    SendKeys,
    It looks like Gerry whimped out on us


  • #7
    Registered
    Join Date
    Apr 2004
    Location
    usa
    Posts
    439
    Downloads
    0
    Uploads
    0
    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
    Community Moderator ger21's Avatar
    Join Date
    Mar 2003
    Location
    Shelby Twp, MI....USA
    Posts
    22,289
    Downloads
    0
    Uploads
    0
    Quote 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.
    Gerry

    Mach3 2010 Screenset
    http://home.comcast.net/~cncwoodworker/2010.html

    (Note: The opinions expressed in this post are my own and are not necessarily those of CNCzone and its management)


  • #9
    Gold Member
    Join Date
    Apr 2003
    Location
    Ohio, USA
    Posts
    1,744
    Downloads
    1
    Uploads
    0
    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
    Registered
    Join Date
    Apr 2003
    Location
    Colorado
    Posts
    9
    Downloads
    0
    Uploads
    0
    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
    Community Moderator ger21's Avatar
    Join Date
    Mar 2003
    Location
    Shelby Twp, MI....USA
    Posts
    22,289
    Downloads
    0
    Uploads
    0
    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.
    Gerry

    Mach3 2010 Screenset
    http://home.comcast.net/~cncwoodworker/2010.html

    (Note: The opinions expressed in this post are my own and are not necessarily those of CNCzone and its management)


  • #12
    *Registered User*
    Join Date
    Jun 2004
    Location
    United States
    Posts
    3
    Downloads
    0
    Uploads
    0
    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
    Attached Files Attached Files


  • Page 1 of 2 12 LastLast

    Posting Permissions


     


    About CNCzone.com

      We are the largest and most active discussion forum from DIY CNC Machines to the Cad/Cam software to run them. The site is 100% free to join and use, so join today!

    Follow us on

    Facebook Dribbble RSS Feed


    Search Engine Friendly URLs by vBSEO ©2011, Crawlability, Inc.