Error Within Nested For Loop

  • So I'm writing this code which determines the number based on values of a, b, and c. I figured a nested for loop was the best approach where there are 10 possible selections for a, 2 for b and 3 for c giving a total number of 60 combinations. When I try to run this though it gives me this:"Complie error: next without for" and highlights the first Next on line 14. From what I can tell, that Next does have a For connected to it but maybe I'm missing something. Any advice or corrections would be appreciated.


    Below is the code, but on here the for's and next's don't line up as they do in my actual code in VBA.


  • Re: Error Within Nested For Loop


    Try this :


  • Re: Error Within Nested For Loop


    You have an open Block If. Change

    Code
    If i = a And j = b And k = c Then 
                        Exit For

    to

    Code
    If i = a And j = b And k = c Then Exit For

    [SIZE=1]Entia non sunt multiplicanda sine necessitate.[/SIZE]

  • Re: Error Within Nested For Loop


    if your original logic was correct, then this works:

    It will not always give the same result as WinteE's suggestion.

    [SIZE=1]Entia non sunt multiplicanda sine necessitate.[/SIZE]

  • Re: Error Within Nested For Loop


    Thank you for the replies. Although the suggestions cleared up a couple of things, the main task at hand was still not solved. Still looking at the code from a previous post, I've attached an excel sheet which may clarify what I'm looking for a little better. Within the sheet, you can select using combo-boxes what you want for a, b and c. And as you can see from the B3:E63, there are 60 possible combinations of the variables. I would like for my code to dtake in these three selections(a, b, and c) and come up with what corresponding number is being looked at.

  • Re: Error Within Nested For Loop


    In that case, there's no need for VBA:


    C2: desired value for a
    D2: desired value for b
    E2: desired value for c


    B2, entered as a array formula (Cntl-Shift-Enter instead of Enter)


    =INDEX(B4:B63,MATCH(C2 & "|" & D2 & "|" & E2, C4:C63 & "|" & D4:D63 & "|" & E4:E63, 0) )

    [SIZE=1]Entia non sunt multiplicanda sine necessitate.[/SIZE]

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!