这个问题不是关于任何应用程序,而是关于找到可行的算法。我有一个将数据从Excel移动到MS Project的代码。我当前的解决方案一直有效,直到中间没有空行为止。
我有从第19行开始的数据。所以19-18是第一个任务,20-18是第二个任务,依此类推
Task 1 (row 19)
Task 2 (row 20)
Task 3 (row 22)
我的代码不起作用,因为MS Project中有任务2,下一个应该是数字3,但22-18是4。有人对此有什么好的解决方案吗?
这是代码:
' Move data to project
For i = 19 To lRow
strValue = WorksheetToOperate.Range("C" & i)
strStartDate = WorksheetToOperate.Range("E" & i)
strEndDate = WorksheetToOperate.Range("F" & i)
Strresource = WorksheetToOperate.Range("J" & i)
' Import tasks
If (WorksheetToOperate.Range("C" & i).Value <> "") And _
(Not IsError(WorksheetToOperate.Range("C" & i).Value)) Then
newproj.Tasks.Add strValue
End If
' Import start date
If (WorksheetToOperate.Range("E" & i).Value <> "") And _
(Not IsError(WorksheetToOperate.Range("E" & i).Value)) Then
newproj.Tasks(i - 18).Start = strStartDate
End If
' Import end date
If (WorksheetToOperate.Range("F" & i).Value <> "") And _
(Not IsError(WorksheetToOperate.Range("F" & i).Value)) Then
newproj.Tasks(i - 18).Finish = strEndDate
End If
' Import recources
If Not ExistsInCollection(newproj.Resources, Strresource) Then _
newproj.Resources.Add.Name = Strresource
If (WorksheetToOperate.Range("J" & i).Value <> "") And _
(Not IsError(WorksheetToOperate.Range("J" & i).Value)) Then
newproj.Tasks(i - 18).ResourceNames = Strresource
End If
Next i
通过使用Task对象变量来跟踪刚刚添加的任务,可以大大简化跳过空行的问题。另外,将With
语句与Worksheet对象一起使用可以进一步简化代码。
Sub CreateSchedule()
Dim prj As MSProject.Application
Set prj = CreateObject("MSProject.Application")
prj.Visible = True
Dim newProj As MSProject.Project
Set newProj = prj.Projects.Add
Dim i As Long
Dim t As MSProject.Task
For i = 19 To 28 'lRow
With WorksheetToOperate
If Not IsEmpty(.Range("C" & i)) Then
Set t = newProj.Tasks.Add(CStr(.Range("C" & i)))
t.Start = CDate(.Range("E" & i))
t.Finish = CDate(Range("F" & i))
t.ResourceNames = CStr(.Range("J" & i))
End If
End With
Next i
End Sub
我已经设法让它与附加变量m
:一起工作
Dim m
m = 0
' Move data to project
For i = 19 To lRow
If IsEmpty(WorksheetToOperate.Range("C" & i).Value) Then
m = m + 1
Else
strValue = WorksheetToOperate.Range("C" & i)
strStartDate = WorksheetToOperate.Range("E" & i)
strEndDate = WorksheetToOperate.Range("F" & i)
Strresource = WorksheetToOperate.Range("J" & i)
' Import tasks
If (WorksheetToOperate.Range("C" & i).Value <> "") And _
(Not IsError(WorksheetToOperate.Range("C" & i).Value)) Then
newproj.Tasks.Add strValue
End If
' Import start date
If (WorksheetToOperate.Range("E" & i).Value <> "") And _
(Not IsError(WorksheetToOperate.Range("E" & i).Value)) Then
newproj.Tasks(i - (18 + m)).Start = strStartDate
End If
' Import end date
If (WorksheetToOperate.Range("F" & i).Value <> "") And _
(Not IsError(WorksheetToOperate.Range("F" & i).Value)) Then
newproj.Tasks(i - (18 + m)).Finish = strEndDate
End If
' Import recources
If Not ExistsInCollection(newproj.Resources, Strresource) Then _
newproj.Resources.Add.Name = Strresource
If (WorksheetToOperate.Range("J" & i).Value <> "") And _
(Not IsError(WorksheetToOperate.Range("J" & i).Value)) Then
newproj.Tasks(i - (18 + m)).ResourceNames = Strresource
End If
End If
Next i