Me again, still plugging away at VBA.
Had a bit of code I was working on for a guy on here, and came across a problem while putting the finishing touches on it. It's since been solved by someone else, but for future reference...
This obviously isn't a functional code in its current state, but it does work of the - Next iFor - is something else, such as - cells(1, 2) = 3Dim iFor As Integer For iFor = 1 To 10 If Cells(1, 1) = 1 Then Next iFor Else Cells(1, 2) = 2 End If Next iFor End Sub
Just wondering why it is everything else seems to work with IF this way, but NEXT keeps getting me errors.
Thanks!
Last edited by mewingkitty; 01-09-2009 at 06:55 PM.
=IF(AND(OR(BLONDE,BRUNETTE,REDHEAD),OR(MY PLACE,HER PLACE),ME),BOW-CHICKA-BOW-WOW,ANOTHER NIGHT ON THE INTERNET)
Your post does not comply with Rule 1 of our Forum RULES. Your post title should accurately and concisely describe your problem, not your anticipated solution. Use terms appropriate to a Google search. Poor thread titles, like Please Help, Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will be addressed according to the OP's experience in the forum: If you have less than 10 posts, expect (and respond to) a request to change your thread title. If you have 10 or more posts, expect your post to be locked, so you can start a new thread with an appropriate title.
To change a Title on your post, click EDIT then Go Advanced and change your title, if 2 days have passed ask a moderator to do it for you.
Sincerely,
Leith Ross
I sometimes forget that although I'm fun and amusing, my thread titles are not allowed the same luxury for the sake of the search engine. Thanks for not hitting me with an infraction.
At any rate, I've been messing with this for a better part of the day, and searching the forum, and the VBA help files. Can't seem to find the format in which you use NEXT to loop back to a FOR statement within an IF.
mew?
=IF(AND(OR(BLONDE,BRUNETTE,REDHEAD),OR(MY PLACE,HER PLACE),ME),BOW-CHICKA-BOW-WOW,ANOTHER NIGHT ON THE INTERNET)
mewingkitty,
Try:
Option Explicit Sub Test() Dim iFor As Integer For iFor = 1 To 10 If Cells(1, 1) <> 1 Then Cells(1, 2) = 2 End If Next iFor End Sub
Have a great day,
Stan
Have a great day,
Stan
stanleydgromjr
Windows Vista Business, Excel 2003 and 2007
If you are satisfied with the solution(s) provided, please mark your thread as Solved by clicking EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.
I had a much longer code with several IF's which, if any of them came back as true, I would want to terminate all following if statements, and proceed with the next NEXT part of the loop.
My reasoning behind using this simplistic example was to save time for anyone looking at it, and make it easier for anyone else with the same question to understand if they found it later.
So...
How do you loop back to a NEXT if a condition is met half way through the code?
=IF(AND(OR(BLONDE,BRUNETTE,REDHEAD),OR(MY PLACE,HER PLACE),ME),BOW-CHICKA-BOW-WOW,ANOTHER NIGHT ON THE INTERNET)
mewingkitty
You can only have 1 Next command for each For command. - Your code has 2 Next commands.
This limit also applies to End If's, End Selects, Do & Wend loops
Please Read Forum Rules Before Posting
Wrap VBA code by selecting the code and clicking the # icon or Read This
How To Cross Post politely
Top Excel links for beginners to Experts
If you are pleased with a member's answer then use the Scales icon to rate it
If my reply has assistedor failed to assist you
I welcome your Feedback.
Alrighty then.
Thanks!
=IF(AND(OR(BLONDE,BRUNETTE,REDHEAD),OR(MY PLACE,HER PLACE),ME),BOW-CHICKA-BOW-WOW,ANOTHER NIGHT ON THE INTERNET)
As only a single Next statement can be used, you can carefully structure the logic tests using If or Select Case statements so that as soon as one condition is met, the "next line of processed code" becomes the Next line. However, if you can't easily do this, place a Label immediately before the Next line & use goto statements, for example
hthif xyz = abc then goto Continue range(123).value = 5 if uvw = def then goto Continue '... Continue: next ifor
Rob
Rob Brockett
Kiwi in the UK
Always learning & the best way to learn is to experience...
Thanks, that's what I was looking for.
Had a list of conditions specific to a particular sheet, and the criteria were somewhat... sketchy, so I wanted an easy out on the IF statements :D
=IF(AND(OR(BLONDE,BRUNETTE,REDHEAD),OR(MY PLACE,HER PLACE),ME),BOW-CHICKA-BOW-WOW,ANOTHER NIGHT ON THE INTERNET)
Beauty, pleased I could help :-)
btw, you may be able to get your code to run much quicker (w/o having seen it) if you move some or all your conditions out of the code & (depending on actual details) put them into a single boolean helper column &/or then limit your range to be looped through using something like Advanced Filter/AutoFilter/xlspecialcells.
If you want someone to glance over your code for potential optimisation you could post a sample file in a new thread (I reckon this one's done & dusted now + you've marked it as solved).
Rob
Rob Brockett
Kiwi in the UK
Always learning & the best way to learn is to experience...
I'll take a little issue with Rob's suggestion. Using GoTo statements for anything other than error handling make code hard to follow, debug, and maintain, and VBA has a wealth of flow control structures to choose from to avoid them. For example,
For i = 1 To 10 If cond1 Then ' do this ElseIf cond2 Then ' do that Else ' do the other thing End If Next i
Last edited by shg; 01-10-2009 at 09:32 PM.
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
I agree with shg that it is best to avoid using Goto's except for error handling
Another option is to built several smaller macros that each do part of what you require and call them from a main macro
Using shg code as an example
For i = 1 To 10 If cond1 Then call macro1 ElseIf cond2 Then call macro2 Else call macro3 End If if newCondTest then call macro4 end if End If
Please Read Forum Rules Before Posting
Wrap VBA code by selecting the code and clicking the # icon or Read This
How To Cross Post politely
Top Excel links for beginners to Experts
If you are pleased with a member's answer then use the Scales icon to rate it
If my reply has assistedor failed to assist you
I welcome your Feedback.
Ooopps!
Yes, my feelings are the same as Shg's & Mudraker's.
I should have put more emphasis on my statement "you can carefully structure the logic tests using If or Select Case statements" (or "smaller macros" as Mudraker suggests) rather than just providing the easy "spaghetti coding" but less structured coding option.
Rob
Rob Brockett
Kiwi in the UK
Always learning & the best way to learn is to experience...
Thanks for all the feedback, I certainly want to learn good practice early on so I don't pick up bad habits as I'm learning all of this. I truly appreciate the open minded feedback from y'all!
mew!
=IF(AND(OR(BLONDE,BRUNETTE,REDHEAD),OR(MY PLACE,HER PLACE),ME),BOW-CHICKA-BOW-WOW,ANOTHER NIGHT ON THE INTERNET)
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks