+ Reply to Thread
Results 1 to 14 of 14
  1. #1
    Valued Forum Contributor mewingkitty's Avatar
    Join Date
    09-29-2008
    Location
    Fort McMurray, Alberta, Canada
    MS-Off Ver
    Excel 2007
    Posts
    949

    FOR, IF, NEXT... Error in VBA while attempting a simple code.

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

    Code:
    Dim 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
    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) = 3

    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 07:55 PM.
    =IF(AND(OR(BLONDE,BRUNETTE,REDHEAD),OR(MY PLACE,HER PLACE),ME),BOW-CHICKA-BOW-WOW,ANOTHER NIGHT ON THE INTERNET)

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,483
    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

  3. #3
    Valued Forum Contributor mewingkitty's Avatar
    Join Date
    09-29-2008
    Location
    Fort McMurray, Alberta, Canada
    MS-Off Ver
    Excel 2007
    Posts
    949

    Sorry about that.

    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)

  4. #4
    Forum Guru
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2003, 2007.
    Posts
    1,412
    mewingkitty,

    Try:

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

  5. #5
    Valued Forum Contributor mewingkitty's Avatar
    Join Date
    09-29-2008
    Location
    Fort McMurray, Alberta, Canada
    MS-Off Ver
    Excel 2007
    Posts
    949

    Not exactly what I was hoping for.

    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)

  6. #6
    Forum Guru mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,984
    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 assisted or failed to assist you I welcome your Feedback.

  7. #7
    Valued Forum Contributor mewingkitty's Avatar
    Join Date
    09-29-2008
    Location
    Fort McMurray, Alberta, Canada
    MS-Off Ver
    Excel 2007
    Posts
    949

    That answers that

    Alrighty then.
    Thanks!
    =IF(AND(OR(BLONDE,BRUNETTE,REDHEAD),OR(MY PLACE,HER PLACE),ME),BOW-CHICKA-BOW-WOW,ANOTHER NIGHT ON THE INTERNET)

  8. #8
    Forum Guru
    Join Date
    01-03-2006
    Location
    Basingstoke, England
    MS-Off Ver
    2007 (work & home)
    Posts
    2,240
    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
    Code:
    if xyz = abc then goto Continue
    range(123).value = 5
    if uvw = def then goto Continue
    '...
    Continue:
    next ifor
    hth
    Rob
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

  9. #9
    Valued Forum Contributor mewingkitty's Avatar
    Join Date
    09-29-2008
    Location
    Fort McMurray, Alberta, Canada
    MS-Off Ver
    Excel 2007
    Posts
    949

    Heeeeeyyyy

    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)

  10. #10
    Forum Guru
    Join Date
    01-03-2006
    Location
    Basingstoke, England
    MS-Off Ver
    2007 (work & home)
    Posts
    2,240
    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...

  11. #11
    Forum Moderator shg's Avatar
    Join Date
    06-21-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007
    Posts
    25,126
    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,
    Code:
        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 10:32 PM.
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  12. #12
    Forum Guru mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,984
    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
    Code:
        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 assisted or failed to assist you I welcome your Feedback.

  13. #13
    Forum Guru
    Join Date
    01-03-2006
    Location
    Basingstoke, England
    MS-Off Ver
    2007 (work & home)
    Posts
    2,240
    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...

  14. #14
    Valued Forum Contributor mewingkitty's Avatar
    Join Date
    09-29-2008
    Location
    Fort McMurray, Alberta, Canada
    MS-Off Ver
    Excel 2007
    Posts
    949

    Much appreciated

    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)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0