Home > OS >  VBA to export excel predecessor data to MS Project
VBA to export excel predecessor data to MS Project

Time:04-19

Currently I have 4 columns in my excel i.e. (ID Column can be ignored)

ID Task Name Resource Names Predecessors
1 Task1 Employee1 2
2 SubTask1 Employee2
3 Task2 Employee1 4,5
4 SubTask2 Employee2
5 SubTask3 Employee2

I am looking to create a Macro button in my Excel to directly export the contents into MS Project. I can only find sharing in the web about transferring the Task ONLY. Is there anyone can help me on this? Really appreciate it.

My existing code (credit to #madschedules) which only include for Tasks.

Sub createNewMSPFromExcelData()

' First go turn on the MS Project reference library

' Declare variables
Dim pjApp As MSProject.Application
Dim pjProject As MSProject.Project
Dim pjtasklist As MSProject.Tasks
Dim pjtask As MSProject.Task
'Dim pjpredlist As MSProject.TaskDependencies
'Dim pjpredlist As TaskDependencies
Dim pjpred As TaskDependency
Dim xlrange As Range
Dim xlrow As Long
Dim counter As Integer

'open MS Project application
Set pjApp = New MSProject.Application
pjApp.Visible = True

' Add a new project file
Set pjProject = pjApp.Projects.Add
Set pjtasklist = pjProject.Tasks


'Loop through all the Excel Data in the worksheet
counter = 2
Do Until Cells(counter, 1) = ""
    Debug.Print Cells(counter, 1).Value & "   " & Cells(counter, 2).Value & "   " & Cells(counter, 3).Value

    ' Add new task into MS Project from Excel Value
    pjtasklist.Add (Cells(counter, 2).Value)
    pjpred.Add (Cells(counter, 3).Value)

    counter = counter   1
Loop

MsgBox ("There are " & pjtasklist.Count & vbNewLine & _
       " tasks in our new project")

End Sub

sample schedule

And finally, here is the code to turn the table above into the schedule:

Sub createNewMSPFromExcelData()

Dim pjApp As MSProject.Application
Dim pjProject As MSProject.Project
Dim pjtasklist As MSProject.Tasks
Dim pjtask As MSProject.Task
Dim counter As Integer

Set pjApp = New MSProject.Application
pjApp.Visible = True

Set pjProject = pjApp.Projects.Add
Set pjtasklist = pjProject.Tasks

counter = 2
Do Until Cells(counter, 1) = ""

    Set pjtask = pjtasklist.Add(Cells(counter, 2).Value)
    pjtask.ResourceNames = Cells(counter, 3).Value
    pjtask.Predecessors = Cells(counter, 4).Value
    pjtask.OutlineLevel = Cells(counter, 5).Value

    counter = counter   1
Loop

MsgBox ("There are " & pjtasklist.Count & vbNewLine & _
       " tasks in our new project")

End Sub
  • Related