+ Reply to Thread
Results 1 to 5 of 5

Declare a FOR LOOP with a range but NOT these values.

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-20-2011
    Location
    United States, Ohio
    MS-Off Ver
    Excel 2010
    Posts
    295

    Question Declare a FOR LOOP with a range but NOT these values.

    I have mutiple FOR LOOPS like:

    FOR blah = 10 TO 20
    'code
    Next blah
    
    FOR blah = 23 TO 35
    'code
    Next blah
    
    FOR blah =39 TO 45
    'code
    Next blah
    and the code is the same. Can I combine those FOR LOOPS and define the intervals from say 10 TO 45 - EXCEPT 22,,36,37,38

    If so - how??
    Last edited by clemsoncooz; 02-19-2012 at 09:20 PM.

  2. #2
    Forum Expert
    Join Date
    03-23-2004
    Location
    London, England
    MS-Off Ver
    Excel 2019
    Posts
    7,076

    Re: Declare a FOR LOOP with a range but NOT these values.

    FOR i = 10 TO 45
    IF i <> 22 AND i <> 36 AND i <>37 AND i <>38 THEN do function1
    END IF
    NEXT i
    Regards
    Special-K

    Ensure you describe your problem clearly, I have little time available to solve these problems and do not appreciate numerous changes to them.

  3. #3
    Forum Contributor
    Join Date
    12-20-2011
    Location
    United States, Ohio
    MS-Off Ver
    Excel 2010
    Posts
    295

    Re: Declare a FOR LOOP with a range but NOT these values.

    Just checking as I havent run it yet, but should the IF statement be ANDs or ORs?

  4. #4
    Forum Expert
    Join Date
    03-23-2004
    Location
    London, England
    MS-Off Ver
    Excel 2019
    Posts
    7,076

    Re: Declare a FOR LOOP with a range but NOT these values.

    Definitely ANDs

    If its ORs...

    Just look at this section of the code if we change it to ORs

    IF i <> 22 OR i <>36... THEN do function

    Lets say i <> 36
    THEN we do the function...CORRECT!

    But let's say i is 22, well thats not = 36 so the function will still get called...INCORRECT!
    We dont want the function called if its 22

    So they're definitely ANDs

  5. #5
    Forum Contributor
    Join Date
    12-20-2011
    Location
    United States, Ohio
    MS-Off Ver
    Excel 2010
    Posts
    295

    Re: Declare a FOR LOOP with a range but NOT these values.

    yeah yeah that makes sense. Thank you so much!

+ Reply to Thread

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