Skip to content

Looping backwards through a 'For Each' collection

John BortonMember for 8 years 1 month
Created
6
3
0

 

Here is the usual VBA code for looping through all tasks in a project:

  For Each t In ActiveProject.Task

      {code for desired action}

  Next t

This loops through from the top down (as far as I can tell).

Anyone have an ideas as to how I can reverse this and go from the bottom up?

Thanks
JB

 

6 replies

Oldest first
  1. 0
    Tom Boyle ·

    [Delete duplicate]

    [Delete duplicate]

     
     
     
     
     
  2. 0
    Tom Boyle ·

    Hi John,  this will do what

    Hi John,  this will do what you ask, though I can think of no good general-use case.  Good luck, tom

    Sub BackTasks()

        Dim t As Task

        Dim i As Long

        For i = ActiveProject.Tasks.Count To 1 Step -1

            Set t = ActiveProject.Tasks(i)

            If Not t Is Nothing Then

                'Your Code Here

            End If

        Next i

    End Sub

     
  3. 0
    John Borton ·

    That's it Tom ...

    That's it Tom ... THANKS.

    The task.count property was the secret and this link didn't list it for whatever reason:
    https://docs.microsoft.com/en-us/office/vba/api/project.task

    A
    s to its usage, I actually just successfully used it to modify your ZFFImpose function.  I've been creating schedules with tasks such that I needed to loop through multiple times to get all the float out of all the faux-ZFF relationships (programatically constraining one task simply moved the float up to the previous task requiring another run through). 

    My previous schedule had a known number of these relationship layers (3) so I just looped through your code three times.  Now I have a schedule that the users might add an unknown number and I needed a more elegant solution than my previous clutz.

    Going through backwards allows your code to now remove the float as far up the chain as needed in one go-round.

    I'm a programming hack, so you might have come up with a cleaner solution, but this works slick as a whistle as written.

    THANKS (for both the original code AND the mod)
    JB
     

  4. 0
    Tom Boyle ·

    John,Glad you got it

    John,

    Glad you got it sorted. 

    In "general use," one can't reliably presume a correlation between the ID sequence and the logical sequence.  Still, as you've explained, this certainly seems to offer a bit of streamlining for that particular (ZFF) issue.

    Rgds, t 

  5. 0
    John Borton ·

    // "In "general use," one

    // "In "general use," one can't reliably presume a correlation between the ID sequence and the logical sequence." //

    Understood.  In this case it's a factory production schedule that by fiat gets used in a particular format over and over so I have pretty tight control on how it's sorted.

    Thanks again.

    JB

  6. 0
    Susanna Kate ·

    Thanks Tom for the code!

    Thanks Tom for the code!

Log in or join to reply

ProjectControls.com is free to join. Members can post questions, reply, vote and follow discussions.

Existing members: your original username and password still work. Forgot password?