0

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.

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

Oh yes, i forgot to mention. It does not let me add a 2nd argument like that. Text becomes red in VBA.
Remove the parentheses since you aren't returning an object. I updated my answer based on your code update.
Thank you very much Rachel. I tried both your solutions and both do exactly what they need to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.