Consulting

Results 1 to 8 of 8

Thread: For Loop with multiple conditions.

  1. #1
    VBAX Regular
    Joined
    Dec 2012
    Posts
    12
    Location

    For Loop with multiple conditions.

    .



    Last edited by Tdude; 08-28-2014 at 10:43 AM.

  2. #2
    VBAX Regular
    Joined
    Dec 2012
    Posts
    12
    Location

    For/next Loop with multiple conditions/ Dynamic Range/offset property

    Dear All,

    a quick one....

    I am dealing with a nested for/next loop but I actually need both i and j to change at the same time and not one at the time.

    something like "For i = 1 To 30 and j=0 to 87" or "For i = 1 To 30, j=0 to 87"

    I'm sure it is easier than it looks but just can't find a way.

    Do you have any suggestions, please?

    Thanks in advance

    the sample code is the following


    [VBA]


    Sub Trial()


    For i = 1 To 30

    For j = 0 To 87 Step 3

    With Range("A1")

    Set myRange = Range(.Offset(1, i + j), .Offset(1, i + j).End(xlDown))

    End With


    Next j

    Next i


    End Sub


    [/VBA]

  3. #3
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    J = -3
    For 1 = 1 to 30
    J = J + 3
    blah blah
    Next I
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by SamT View Post
    J = -3
    For 1 = 1 to 30
    J = J + 3
    blah blah
    Next I
    Do you mean

    For i = 1 To 30
        j = i * 3 - 3
        'etc
    Next i
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    mathematically identical results

    But mine's faster :LOL
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Sorry, it was the line

    For 1 = 1 To 30
    which I was suggesting that you really meant

    For i = 1 To 30
    in your code that I was remarking upon, I just changed the way j was set as it seemed more intuitive (at least to me it did).
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    VBAX Regular
    Joined
    Dec 2012
    Posts
    12
    Location
    Thank you both for your help !!

  8. #8
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Yes, I meant i =1 to 30

    My finger always reaches first for the 1 when I use lower case I

    The way Tdude worded his Q made that J = J +3 jump into my (weak, medically abused, and pain filled) head. After I had it in print I looked askance at it for a while thinking.

    I came to the conclusion that is is actually a bit faster than using a multiplier on the outer loop counter.

    I'm so happy now! I saved 30 seconds/petaflop!
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

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