0

I have few tasks in ms project. They are grouped with few summary tasks, so the progress of work completed is tracked by those summary tasks. I need to add one separate single task which will summarize all progress from some of the summary tasks and some of single tasks. All tasks that need to be summarized will be linked as predecessors for this final summary task.

I've wrote code like this bellow but I'm struggling to make it work as intended.

Sub SumProgress()

Dim t As Task, subt As Task
Dim NumSub As Integer, TotalProgres As Integer, TaskProgres As Integer
Set area = ActiveProject.Tasks

For Each t In area
        If t.Flag10 = True Then
            NumSub = Int(t.PredecessorTasks.Count)
            For Each subt In t.PredecessorTasks
                TotalProgres = TotalProgres + Int(subt.PercentComplete)
            Next subt
            t.PercentComplete = TotalProgress / NumSub
        End If
Next t


End Sub
  • 1) "struggling to make it work" is not specific enough. Give details on what you expected and what you got. 2) TotalProgres should be reset to 0 inside the loop (after Flag10 = True). – Rachel Hettinger Mar 18 '21 at 15:40
  • 1. I expect that task named ProgressSum will summarize all progresses from tasks: option a) linked to ProgressSum task as predecessors option b) which have exact the same character in text5 field ``` Structure looks like this: | 33% Task-1 | 50% Task-2 | 0% Task-3 | 75% Task-4 | Those tasks are not linked, each one of them is separate and I want to summarize their progress in one additional task called ProgressSum. – Manioza Mar 18 '21 at 20:03

1 Answers1

0

There are two bugs in the code:

  1. TotalProgres was not reset to 0 before each new progress summary task was calculated.

  2. The variable was declared as "TotalProgres" but later it is used as "TotalProgress", different spelling = different variable. Always use the Option Explicit to easily avoid this kind of bug. See What do Option Strict and Option Explicit do?

Here is the code with those issues fixed:

Option Explicit

Sub SumProgress()

Dim area As Tasks
Dim t As Task, subt As Task
Dim NumSub As Integer, TotalProgress As Integer
Set area = ActiveProject.Tasks

For Each t In area
    If Not t Is Nothing Then
        If t.Flag10 = True Then
            TotalProgress = 0
            NumSub = Int(t.PredecessorTasks.Count)
            For Each subt In t.PredecessorTasks
                TotalProgress = TotalProgress + Int(subt.PercentComplete)
            Next subt
            t.PercentComplete = TotalProgress / NumSub
        End If
    End If
Next t

End Sub
Rachel Hettinger
  • 7,927
  • 2
  • 21
  • 31
  • Thank you for spell check but this code is crashing on | If t.Flag10 = True Then | and it's still not doing what intended... Runtime error 91... – Manioza Mar 18 '21 at 20:36
  • Sounds like you have blank lines in your schedule; not sure why you weren't getting that error with your original code. See updated code. – Rachel Hettinger Mar 18 '21 at 21:27
  • Yeap, I've used you updated code, and this is the error code which I'm getting. The Flag is just to show the script on which tasks it should be done, so those tasks are filled for sure. – Manioza Mar 19 '21 at 11:09
  • Note: PercentComplete is Read Only on summary tasks, so only set Flag10 on non-summary tasks, otherwise you'll get Error 1101. `If t.Flag10 = True Then` will raise Error 91 Object Variable not set if t is Nothing (e.g. if there are *any* blank rows in the schedule). The posted code works. – Rachel Hettinger Mar 19 '21 at 17:22
  • Worked... without changing anything... o.O wtf MSP! xD but I'm glad it tid. – Manioza Mar 20 '21 at 16:38