+ Reply to Thread
Results 1 to 5 of 5

Need help identifying a value range for ending a Loop Until

  1. #1
    Registered User
    Join Date
    06-01-2013
    Location
    boston, ma
    MS-Off Ver
    Excel 2003
    Posts
    3

    Question Need help identifying a value range for ending a Loop Until

    Hi. I have virtually no programming experience. I used the Macro Recorder to produce this code to generate a column of random numbers and then sort several columns of data by those numbers, effectively randomizing them. I poked around some discussion boards and was able to learn how to set up a rudimentary Do… Loop Until so the macro will run until a cell auto-populated by a selection of the data being randomized reaches a criterion value.

    The macro does run OK, but my problem is I need to be able to define a range this is at the same time both above one value and below another value (e.g., both > -0.0055 and < 0.0055), and right now I can only do one or the other (e.g., either > -0.0055 or < 0.0055). I’d also like to know how to define a loop stopping criterion based on values from two different cells, or on ranges from two different cells.

    This should be an easy fix for someone with some experience with VBA. Thanks in advance. Here is my code:

    Please Login or Register  to view this content.
    Last edited by JBeaucaire; 06-04-2013 at 07:20 PM. Reason: Added CODE tags and corrected title, as per Forum Rules. Take a moment to read the Forum Rules in the menu bar above. Thank

  2. #2
    Registered User
    Join Date
    04-30-2013
    Location
    Brussels
    MS-Off Ver
    Excel 2007
    Posts
    35

    Re: Need help identifying a value range for ending a Loop Until

    Dear erik1,

    You can add "OR" and "AND" statements in a Loop Until statement.

    In your case, your code would be:

    Please Login or Register  to view this content.
    You can add any criteria (other ranges, other values, variables, any conditions, basically)

    Have fun !
    ExcelTab.com

  3. #3
    Registered User
    Join Date
    06-01-2013
    Location
    boston, ma
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Need help identifying a value range for ending a Loop Until

    Thanks, ExcelTab. I replaced my old Loop Until line with your suggested code and it works, but to get what I specifically was looking for I had to replace your "Or" with an "And." I'm a bit embarrassed because I could've sworn I had tried that already, but there you go. In the meantime I found an alternate fix by using an "IF-AND" [=IF(AND(L433>-0.0055,M433< 0.0055), "Yes","No"] to define a single condition to use as a stop for the macro, but yours of course is the more elegant solution, and I'll switch to that. One additional question: If I wanted to use an "And" along with an "Or" in the same Loop Until, for example--

    Loop Until Range("L433").Value > -0.0055 And Range("L433").Value < 0.0055 Or Range("K433").Value>-0.055 And Range("K433").Value < 0.055

    would I have to use some parentheses or brackets or something around the 2nd half or the statement? Thanks in advance.

  4. #4
    Registered User
    Join Date
    06-01-2013
    Location
    boston, ma
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Need help identifying a value range for ending a Loop Until

    Oops, in the last post I meant:

    "In the meantime I found an alternate fix by using an "IF-AND" [=IF(AND(L433>-0.0055,L433< 0.0055), "Yes","No"] to define a single condition to use as a stop for the macro..."

  5. #5
    Registered User
    Join Date
    04-30-2013
    Location
    Brussels
    MS-Off Ver
    Excel 2007
    Posts
    35

    Re: Need help identifying a value range for ending a Loop Until

    Hello erik1,

    I will try to explain you the logic of the position of the AND / OR statements:

    First case:
    Please Login or Register  to view this content.
    This will run until either:
    • A And B are both true
    • C is true

    Second case:
    Please Login or Register  to view this content.
    This will run until either:
    • A And B are both true
    • A And C are both true

    Hope this helps.

    Have fun!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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