我正在尝试从excel'数据库中构建一个简单的计划。与MS Project。
我遇到了多个运行时错误,但我并不在乎这么多,我只是想了解如何将我已经拉入变量的资源分配给任务。我可以将资源添加到项目中,但我不知道如何将它分配给给定的任务。这就是我所拥有的:
Sub ExcelToProjectProto()
Dim oPrjApp, oPrj As Object
Set oPrjApp = MSProject.Application
Dim strJobName, strForeman, strCrane, strLocation, strPieces As String
Dim dStart, dEnd As Date
Dim lRow, lCol, lRevenue As Long
Dim rsCrane, rsForeman As Resource
oPrjApp.Visible = True
Set oPrj = oPrjApp.Projects.Add
oPrj.Title = "Test"
For lRow = 2 To Cells(Rows.Count, 1).End(xlUp).Row
'pulling data from excel
strJobName = Cells(lRow, 1).Value
strCrane = Cells(lRow, 2).Value
strForeman = Cells(lRow, 3).Value
dStart = Cells(lRow, 4).Value
dEnd = Cells(lRow, 6).Value
lRevenue = Cells(lRow, 8).Value
strPieces = Cells(lRow, 9).Value
strLocation = Cells(lRow, 10).Value
oPrj.Tasks.Add.Name = strJobName & "- " & strLocation & " (" & strPieces & " ea)"
oPrj.Tasks(lRow - 1).Start = dStart
oPrj.Tasks(lRow - 1).Finish = dEnd
If Not ExistsInCollection(oPrj.Resources, strCrane) Then oPrj.Resources.Add.Name = strCrane
If Not ExistsInCollection(oPrj.Resources, strForeman) Then oPrj.Resources.Add.Name = strForeman
'Set rsCrane = oPrj.Resources(strCrane) ''trying to setup the resources, I get an error here
'Set rsForeman = oPrj.Resources(strForeman) '' same thing as above line.
'trying to assign a resource
'oPrj.Tasks(lRow - 1).Assignments.ResourceName.Add strCrane
'oPrj.Tasks(lRow - 1).Assignments.ResourceName.Add strForeman
Next
Set oPrjApp = Nothing
Set oPrj = Nothing
End Sub
答案 0 :(得分:0)
我找到了一个方法。
这绝对不是最好的方法,但在我添加这些行中的资源之后:
If Not ExistsInCollection(oPrj.Resources, strCrane) Then oPrj.Resources.Add.Name = strCrane
If Not ExistsInCollection(oPrj.Resources, strForeman) Then oPrj.Resources.Add.Name = strForeman
然后我必须遍历我添加的所有资源并匹配当前的资源并将它们分别设置为rsCrane和rsForeman属性:
For i = 1 To 100
If oPrj.Resources(i).Name = strCrane Then
Set rsCrane = oPrj.Resources(i)
Exit For
End If
Next i
For i = 1 To 100
If oPrj.Resources(i).Name = strForeman Then
Set rsForeman = oPrj.Resources(i)
Exit For
End If
Next i
然后我使用以下命令分配:
rsCrane.Assignments.Add TaskID:=(lRow - 1)
rsForeman.Assignments.Add TaskID:=(lRow - 1)