CNCzone.com-The Largest Machinist Community on the net!



Home Page Mark Forums Read Today's Posts My Replies Classifieds Reviews Photo Gallery Web Links Share Files Advertise With Us Ad List
Go Back   CNCzone.com-The Largest Machinist Community on the net! > Machine Controllers Software and Solutions > Visual Basic


Visual Basic Discuss Visual Basic programing.


Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 03-04-2005, 07:23 PM
ger21's Avatar
Community Moderator
 
Join Date: Mar 2003
Location: Shelby Twp, MI....USA
Posts: 19,570
ger21 is on a distinguished road
Buy me a Beer?
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)
Tweet this Post!Share on Facebook
Reply With Quote

  #2  
Old 03-04-2005, 08:40 PM
Gold Member
 
Join Date: Apr 2003
Location: Ohio, USA
Posts: 1,734
Ken_Shea is on a distinguished road

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
Tweet this Post!Share on Facebook
Reply With Quote

  #3   Ban this user!
Old 03-04-2005, 09:04 PM
 
Join Date: Apr 2004
Location: usa
Posts: 439
sendkeys is on a distinguished road

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.
Tweet this Post!Share on Facebook
Reply With Quote

  #4  
Old 03-04-2005, 10:43 PM
ger21's Avatar
Community Moderator
 
Join Date: Mar 2003
Location: Shelby Twp, MI....USA
Posts: 19,570
ger21 is on a distinguished road
Buy me a Beer?

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)
Tweet this Post!Share on Facebook
Reply With Quote

  #5  
Old 03-05-2005, 12:40 AM
Gold Member
 
Join Date: Apr 2003
Location: Ohio, USA
Posts: 1,734
Ken_Shea is on a distinguished road

OK,
Do While X < 50

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

' Do STuff here

End If

X = X + 1

Loop
Tweet this Post!Share on Facebook
Reply With Quote

Sponsored Links
  #6  
Old 03-05-2005, 12:49 AM
Gold Member
 
Join Date: Apr 2003
Location: Ohio, USA
Posts: 1,734
Ken_Shea is on a distinguished road

SendKeys,
It looks like Gerry whimped out on us
Tweet this Post!Share on Facebook
Reply With Quote

  #7   Ban this user!
Old 03-05-2005, 02:09 AM
 
Join Date: Apr 2004
Location: usa
Posts: 439
sendkeys is on a distinguished road

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.
Tweet this Post!Share on Facebook
Reply With Quote

  #8  
Old 03-05-2005, 07:35 AM
ger21's Avatar
Community Moderator
 
Join Date: Mar 2003
Location: Shelby Twp, MI....USA
Posts: 19,570
ger21 is on a distinguished road
Buy me a Beer?

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)
Tweet this Post!Share on Facebook
Reply With Quote

  #9  
Old 03-05-2005, 09:49 AM
Gold Member
 
Join Date: Apr 2003
Location: Ohio, USA
Posts: 1,734
Ken_Shea is on a distinguished road

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
Tweet this Post!Share on Facebook
Reply With Quote

  #10   Ban this user!
Old 03-17-2005, 01:29 AM
 
Join Date: Apr 2003
Location: Colorado
Posts: 9
drolley is on a distinguished road

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
Tweet this Post!Share on Facebook
Reply With Quote

Sponsored Links
  #11  
Old 03-17-2005, 06:59 AM
ger21's Avatar
Community Moderator
 
Join Date: Mar 2003
Location: Shelby Twp, MI....USA
Posts: 19,570
ger21 is on a distinguished road
Buy me a Beer?

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)
Tweet this Post!Share on Facebook
Reply With Quote

  #12  
Old 03-17-2005, 11:50 AM
*Registered User*
 
Join Date: Jun 2004
Location: United States
Posts: 3
TCARPENTER is on a distinguished road

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
File Type: zip mdlArray.zip‎ (15.0 KB, 134 views)
Tweet this Post!Share on Facebook
Reply With Quote

Reply




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On





All times are GMT -5. The time now is 08:49 PM.





Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Content Relevant URLs by vBSEO
Template-Modifications by TMS

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353