« Survey - Tell me what you like | Main | Calculating a Critical Path Schedule using CPM »

MS Project VBA - Earliest Predecessor

Sometimes we want Project to calculate a schedule a little differently than it does naturally. At least a few times I've had people ask if it is possible to set the start of a specific task based on the date the first it's predecessors completes. With a little code it is easily possible. This code takes the predecessors of a selected tasks and figures out which is the one which will finish first. Then it applies negative lag to the dependencies between itself and the other predecessors. The comments in the code (lines starting with an apostrophe ') describe what is being done

Sub FollowEarliestPredecessor()
Dim t, p, earliest As Task
Dim ps As Tasks
Dim l As Long
Dim tdeps As TaskDependencies
Dim tdep As TaskDependency

Set t = ActiveSelection.Tasks(1)

'Set lag to 0 to remove any previous lags
Set tdeps = t.TaskDependencies
For Each tdep In tdeps
If tdep.To = t.ID Then
tdep.lag = 0
End If
Next tdep
CalculateProject

'Find earliest predecessor
Set ps = t.PredecessorTasks
Set earliest = ps(1)
For Each p In ps
If p.Finish <= earliest.Finish Then
Set earliest = p
End If
Next p

'Set lag so it covers the greatest task variance
l = -Application.DateDifference(earliest.Finish, t.Start)

'Apply that lag to all predecessors except for the earliest
For Each tdep In tdeps
If tdep.To = t.ID And tdep.From <> earliest.ID Then
tdep.lag = l
End If
Next tdep
CalculateProject

End Sub

Pretty simple.

RELATED POSTS

About

The previous article is Survey - Tell me what you like.

The next article is Calculating a Critical Path Schedule using CPM.

Current articles are in the main index page and you can find a complete list of articles in the archives.

Creative Commons License
This weblog is licensed under a Creative Commons License.
Powered by
Movable Type 3.34