Home > OS >  Adding a Task in MS Project at a specific location with VBA through Excel
Adding a Task in MS Project at a specific location with VBA through Excel

Time:03-25

I have and Excel file with all current tasks. New tasks maybe be added at any position. I want to update my MS Project file from Excel by adding missing tasks. Using .Tasks.Add("name from Excel") will as a new Task in MS Project at the bottom. How can i add new Task at a specific location in the middle using VBA from Excel? Can I use SelectRow and InsertTask in VBA code from Excel somehow?

*EDIT Forgot to mention, .Tasks.Add does not let me use a 2nd argument, like it says in documentation.

With mpp
    .Tasks.Add ("name from Excel", RowNumber)
End With

This code will have the line with .Tasks.Add ("name from Excel", RowNumber) in red. If i remove the 2nd argument, it's fine and works.

CodePudding user response:

A quick check of the docs for Tasks.Add shows that the method takes an optional second argument Before:

The position of the task in its containing collection. The default value is the position of the last item in the collection. (data type Long)

Utilize the second argument and pass in the ID of the task before which the new task should be added. For example:

With mpp
    .Tasks.Add "name from Excel", RowNumber
End With

Or

Dim t As MSProject.Task
With mpp
    Set t = .Tasks.Add("name from Excel", RowNumber)
End With
  • Related