+ Reply to Thread
Results 1 to 10 of 10

For inside another For, when condition reached in "For inside" make "For outside" exit

  1. #1
    Registered User
    Join Date
    09-12-2013
    Location
    Leiria, Portugal
    MS-Off Ver
    Excel 2010
    Posts
    11

    For inside another For, when condition reached in "For inside" make "For outside" exit

    Greetings, im very noob so please be gentle.

    I have this:

    For i = 1 To 200

    .
    .
    .

    atuais = Sheets("Geral").Range("$L$12:$L$32").Value
    velhos = Sheets("Geral").Range("$H$12:$H$32").Value

    'Compare 2 columns (value by value)

    For j = LBound(atuais) To UBound(velhos)

    If ((atuais(j, 1) > velhos(j, 1)) And (atuais(j, 1) - velhos(j, 1) < 0.01)) Or ((atuais(j, 1) < velhos(j, 1)) And (velhos(j, 1) - atuais(j, 1) < 0.01)) Or
    (atuais(j, 1) = velhos(j, 1)) Then Exit For

    Next j

    Next i

    What I would like to happen is:
    -It verifies the "If condition" for all the values on the rows of "atuais" and "velhos"
    -If the condition is verified, all the values are almost the same (error of 0.01) or equal to each other, exits the "For i", right now it only exits de "For j".

    Cheers

  2. #2
    Forum Contributor
    Join Date
    04-24-2007
    Location
    NYC
    MS-Off Ver
    2k3/2k7/2010
    Posts
    270

    Re: For inside another For, when condition reached in "For inside" make "For outside" exit

    You have 2 For Loops. For #1 is "j", For #2 is "i". "Exit For" affects the first For loop it sees.

    You need a 2nd if statement to check if "J" finished early and exit "I" if it does.


    Please Login or Register  to view this content.
    Also
    ABS() = Absolute Value, so you don need to worry about negatives.

    This makes it shorter and easier to read.

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    09-12-2013
    Location
    Leiria, Portugal
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: For inside another For, when condition reached in "For inside" make "For outside" exit

    I've done this:

    'Put between Next j and Next i
    if J <> ubound(velhos) then exit for

    but it doesnt do any loop in the "For i", it exits the "For i" in i=1.
    Thanks for the abs tip

  4. #4
    Forum Contributor
    Join Date
    04-24-2007
    Location
    NYC
    MS-Off Ver
    2k3/2k7/2010
    Posts
    270

    Re: For inside another For, when condition reached in "For inside" make "For outside" exit

    ??? I think i am missing something from your code because:

    Please Login or Register  to view this content.
    Will exit once J finds a match. I don't know what your "I" is checking since that loop only looks at "J".

  5. #5
    Registered User
    Join Date
    09-12-2013
    Location
    Leiria, Portugal
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: For inside another For, when condition reached in "For inside" make "For outside" exit

    Every time it does "For i" it copies and pastes values of velhos, dont know if it's because of that.

    Here is the workbook, the sheet "Geral" is where are the columns "atuais" and "velhos", there is only one macro, and it's in the end of it this "For i" part.

    Basically what macro does is: I give some temperatures, after some calculous it gives me others that I put again on the same place of the first temperatures, and i want it to do this until that 0.01 error. I know its working because at the end the values of "velhos" and "atuais" are the same, but it doesnt stop "For i" when it reachs that, so "i" is going to be always the maximum value i put (if is i=1 to 200, its 200).
    Attached Files Attached Files
    Last edited by varreddor; 09-25-2013 at 05:34 PM.

  6. #6
    Forum Contributor
    Join Date
    04-24-2007
    Location
    NYC
    MS-Off Ver
    2k3/2k7/2010
    Posts
    270

    Re: For inside another For, when condition reached in "For inside" make "For outside" exit

    Does Solver have a random number generator? Because your "I" for 1 to 10 will run the same calculation 10 times.

  7. #7
    Registered User
    Join Date
    09-12-2013
    Location
    Leiria, Portugal
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: For inside another For, when condition reached in "For inside" make "For outside" exit

    I want it to be moreless I=1 To 200, i was only testing so it wont take that long, as it doesnt stop . I dont understand ur question, but it's not random. It's to do the same calculation every time, but with different values, it does one time with some values i give, then the solver give me other same amount of values, and those i put as new values for calculation and solver give another values i put again for solver... etc.. etc.. 200 times, or the times when the values i insert (copy/paste) are equal to the values solver give... just that!

  8. #8
    Forum Contributor
    Join Date
    04-24-2007
    Location
    NYC
    MS-Off Ver
    2k3/2k7/2010
    Posts
    270

    Re: For inside another For, when condition reached in "For inside" make "For outside" exit

    How are you changing the value when it goes from 1 to 2, then 2 to 3? because for i = 1 to 10 it does not stop. And each time it uses the same answers.

  9. #9
    Registered User
    Join Date
    09-12-2013
    Location
    Leiria, Portugal
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: For inside another For, when condition reached in "For inside" make "For outside" exit

    If you try to run the macro with I=1 To 10 and without (If J <> UBound(velhos) Then Exit For), you'll see in sheet "Geral" for example the first value of the column "Velhos" is very similar to "Atuais". After some more iterations it's going to become the same value, and that's when i want it to stop. Maybe this part of the code is not doing what i pretend, comparing those values:

    For j = LBound(atuais) To UBound(velhos)

    If (Abs(atuais(j, 1) - velhos(j, 1)) < 0.01) Or (atuais(j, 1) = velhos(j, 1)) Then Exit For

    Next j

  10. #10
    Registered User
    Join Date
    09-12-2013
    Location
    Leiria, Portugal
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: For inside another For, when condition reached in "For inside" make "For outside" exit

    I use copy - paste or pastespecial.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 0
    Last Post: 09-26-2012, 02:18 PM
  2. Can't execute a macro whit text inside procedure header's "()" *Help*
    By Timestamp in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-24-2012, 06:55 AM
  3. Naming a range inside a formula using "Address"
    By mcke1610 in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 02-23-2007, 12:41 PM
  4. If changed array formula reduce ""\""\""\ - signs to #Missing, will it make ...
    By Maria J-son in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-05-2006, 08:25 AM
  5. [SOLVED] How do I put a "date & time saved" stamp inside an Excel Wksht?
    By Bill in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-12-2005, 12:06 PM

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.6.0 RC 1