View Full Version : Link last task


JoJa15
01-02-2006, 08:15 PM
I am trying to find the latest finish date for a group of tasks
assigned to a certain resource and then to link that task to another
one as a predecessor.

So for example say I had a task called "Example"
I want to scan through all tasks in a project plan that belong to
person "A", find the one that has the latest finish date, and then link
that task to "Example" as a predeccesor.

I am assuming I would structure it this way:

(1) In a for loop, loop through all active tasks in the project.
(2) If current task in loop has resource = "a" then assign variable
Finish to finish date of that task.
(3) If Finish is greater then Final_Finish variable assign Finish to
Final Finish and assign ID of task to Final_Finish_ID.
(4) Keep looping.
(5) Assign Final_Finish_ID to the Predeccesor field of task "Example"

Ideally I would like to do this for each resource in the project so I
am assuming that I would have the above loop inside of another loop
that goes through each resource in the project. The "Example" task
would actually be a task with the person's name.

Is the above the easiest way to accomplish what i am trying to achieve
or is there an easier way? I have never programmed Project VBA so it
might take me a little while to figure out the language.

- John

John
01-02-2006, 11:47 PM
OK, I got it to work with the following code. Is there an easier and quicker
way to do this?

Sub Last_Task_Link()

'If project is empty, alert the user and end the macro
If ActiveProject.Tasks.Count = 0 Then
MsgBox MSG_NO_TASKS, Buttons:=vbCritical + R_TO_L,
Title:=Application.Name
End
End If


Dim Temp As Long, Temp2 As Long
Dim R As Long, Names As String, FinishDate As Date, FinishID As String

For R = 1 To ActiveProject.Resources.Count
FinishID = 0
FinishDate = 0
Names = ActiveProject.Resources(R).Name
For Temp = 1 To ActiveProject.Tasks.Count
If Not ActiveProject.Tasks(Temp) Is Nothing Then
If StrComp(Names, ActiveProject.Tasks(Temp).ResourceNames, 1) =
0 Then
If DateValue(ActiveProject.Tasks(Temp).Finish) >=
DateValue(FinishDate) Then
FinishDate = ActiveProject.Tasks(Temp).Finish
FinishID = ActiveProject.Tasks(Temp).ID
End If
End If
End If
Next Temp
For Temp2 = 1 To ActiveProject.Tasks.Count
If Not ActiveProject.Tasks(Temp2) Is Nothing Then
If StrComp(Names, ActiveProject.Tasks(Temp2).Name, 1) = 0 Then
If Not FinishID = 0 Then
ActiveProject.Tasks(Temp2).Predecessors = FinishID
End If
End If
End If
Next Temp2
Next R

End Sub

- John

Mike Glen
01-03-2006, 10:41 AM
Hi John,

Next time, please try posting on the developer newsgroup as this one's
closing down. Please see FAQ Item: 24. Project Newsgroups. FAQs,
companion products and other useful Project information can be seen at this
web address: http://project.mvps.org/faqs.htm

Mike Glen
Project MVP


John wrote:
> OK, I got it to work with the following code. Is there an easier and
> quicker way to do this?
>
> Sub Last_Task_Link()
>
> 'If project is empty, alert the user and end the macro
> If ActiveProject.Tasks.Count = 0 Then
> MsgBox MSG_NO_TASKS, Buttons:=vbCritical + R_TO_L,
> Title:=Application.Name
> End
> End If
>
>
> Dim Temp As Long, Temp2 As Long
> Dim R As Long, Names As String, FinishDate As Date, FinishID As String
>
> For R = 1 To ActiveProject.Resources.Count
> FinishID = 0
> FinishDate = 0
> Names = ActiveProject.Resources(R).Name
> For Temp = 1 To ActiveProject.Tasks.Count
> If Not ActiveProject.Tasks(Temp) Is Nothing Then
> If StrComp(Names, ActiveProject.Tasks(Temp).ResourceNames,
> 1) = 0 Then
> If DateValue(ActiveProject.Tasks(Temp).Finish) >=
> DateValue(FinishDate) Then
> FinishDate = ActiveProject.Tasks(Temp).Finish
> FinishID = ActiveProject.Tasks(Temp).ID
> End If
> End If
> End If
> Next Temp
> For Temp2 = 1 To ActiveProject.Tasks.Count
> If Not ActiveProject.Tasks(Temp2) Is Nothing Then
> If StrComp(Names, ActiveProject.Tasks(Temp2).Name, 1) = 0
> Then If Not FinishID = 0 Then
> ActiveProject.Tasks(Temp2).Predecessors = FinishID
> End If
> End If
> End If
> Next Temp2
> Next R
>
> End Sub
>
> - John