VBA 将 excel 前导数据导出到 MS Project
VBA to export excel predecessor data to MS Project
目前我的 excel 中有 4 列,即(可以忽略 ID 列)
ID
Task Name
Resource Names
Predecessors
1
Task1
Employee1
2
2
SubTask1
Employee2
3
Task2
Employee1
4,5
4
SubTask2
Employee2
5
SubTask3
Employee2
我想在我的 Excel 中创建一个宏按钮,以直接将内容导出到 MS Project 中。我只能在网上找到关于仅转移任务的分享。有没有人可以帮我解决这个问题?非常感谢。
我现有的代码(归功于#madschedules)仅包含任务。
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
expected data in MS Project image
回答前辈们的问题,我目前正在做一个电子原理图设计的项目,涉及多层次结构。因此,要在层次结构上工作,我需要确保在继续下一个级别之前完成子层次结构活动,因此我将原理图导出的前辈详细信息放入 excel。
添加任务然后设置任务属性的关键是在添加后获取对任务对象的引用。所以代替:
pjtasklist.Add (Cells(counter, 2).Value)
做
Set pjtask = pjtasklist.Add (Cells(counter, 2).Value)
pjtask.ResourceNames = Cells(counter, 3).Value
然而,在继续之前,需要明确任务、子任务和前置任务。在调度中,子任务被认为是摘要任务下的任务。摘要任务用于对项目的相关任务子集进行分组。摘要任务的开始和完成日期是根据其子任务的最早开始和最晚完成计算的。
前置任务是必须在另一个任务开始之前发生的任务。几乎总是应该首先输入前置任务,以便按时间顺序读取时间表。因此,前任 ID 值高于其自身的任务极为罕见。相反,Excel table 会更好,其中 OL = OutlineLevel:
ID
Task Name
Resource Names
Predecessors
OL
1
Task1
Employee1
1
2
SubTask1
Employee2
2
3
Task2
Employee1
1
4
SubTask2
Employee2
2
5
SubTask3
Employee2
4
2
这会产生像这样的典型计划结构:
最后,这里是将上面的 table 变成时间表的代码:
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
目前我的 excel 中有 4 列,即(可以忽略 ID 列)
ID | Task Name | Resource Names | Predecessors |
---|---|---|---|
1 | Task1 | Employee1 | 2 |
2 | SubTask1 | Employee2 | |
3 | Task2 | Employee1 | 4,5 |
4 | SubTask2 | Employee2 | |
5 | SubTask3 | Employee2 |
我想在我的 Excel 中创建一个宏按钮,以直接将内容导出到 MS Project 中。我只能在网上找到关于仅转移任务的分享。有没有人可以帮我解决这个问题?非常感谢。
我现有的代码(归功于#madschedules)仅包含任务。
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
expected data in MS Project image
回答前辈们的问题,我目前正在做一个电子原理图设计的项目,涉及多层次结构。因此,要在层次结构上工作,我需要确保在继续下一个级别之前完成子层次结构活动,因此我将原理图导出的前辈详细信息放入 excel。
添加任务然后设置任务属性的关键是在添加后获取对任务对象的引用。所以代替:
pjtasklist.Add (Cells(counter, 2).Value)
做
Set pjtask = pjtasklist.Add (Cells(counter, 2).Value)
pjtask.ResourceNames = Cells(counter, 3).Value
然而,在继续之前,需要明确任务、子任务和前置任务。在调度中,子任务被认为是摘要任务下的任务。摘要任务用于对项目的相关任务子集进行分组。摘要任务的开始和完成日期是根据其子任务的最早开始和最晚完成计算的。
前置任务是必须在另一个任务开始之前发生的任务。几乎总是应该首先输入前置任务,以便按时间顺序读取时间表。因此,前任 ID 值高于其自身的任务极为罕见。相反,Excel table 会更好,其中 OL = OutlineLevel:
ID | Task Name | Resource Names | Predecessors | OL |
---|---|---|---|---|
1 | Task1 | Employee1 | 1 | |
2 | SubTask1 | Employee2 | 2 | |
3 | Task2 | Employee1 | 1 | |
4 | SubTask2 | Employee2 | 2 | |
5 | SubTask3 | Employee2 | 4 | 2 |
这会产生像这样的典型计划结构:
最后,这里是将上面的 table 变成时间表的代码:
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