【问题标题】:Generate MS-project file with main tasks and milestones from excel从 Excel 生成包含主要任务和里程碑的 MS 项目文件
【发布时间】:2019-03-11 08:50:07
【问题描述】:

我设法制作了一个不错的脚本,可以从 excel 中的选定表生成 MS 项目文件。我现在正在寻求帮助,使其更有用。我想在 Excel 中特定表格的每个主要任务下插入里程碑。每个主要任务都有一个对应的里程碑表。

Sub MSPexport()

Dim pjapp As Object
Dim strValue, strWorktime, strMilestone As String
Dim newproj
Set pjapp = CreateObject("MSProject.application") 

If pjapp Is Nothing Then
MsgBox "Project is not installed"
End
End If

pjapp.Visible = True
Set newproj = pjapp.Projects.Add
Set ActiveProject = newproj

pjapp.NewTasksStartOn

'insert tasks here

 For I = 3 To 8 'currently I am pointing to the range A3:A:8 - would like to make it a named range instead - ie "Maintasks" - how to do this?

    strValue = Worksheets("Planning").Range("A" & I)
    newproj.Tasks.Add (strValue)

    'Insert predecessor if not first task
    If I <> 3 Then
      newproj.Tasks(I - 2).Predecessors = (I - 3)
    End If

    'here I would like to insert milestones as subtasks

    For M = 3 to 5 ' this I also would like to be a named range and also I need to check for or lookup the correct main task and the corresponding milestone list
      strMilestone = Worksheets("Milestones").Range("C" & M)
        newproj.Tasks.Add (strMilestone)
        newproj.Tasks(M - 2).Duration = 0
        newproj.Tasks(M - 2).OutlineIndent
        newproj.Tasks(M - 2).Predecessors = (I - 26)
    Next M   
Next I

End Sub

完成后,MS-Project 应如下所示:

【问题讨论】:

    标签: excel vba ms-project


    【解决方案1】:

    这是更新为 1) 使用命名范围和 2) 插入里程碑的代码:

    Sub MSPexport()
    
    Dim pjapp As Object
    Dim newproj As Object
    
    Set pjapp = CreateObject("MSProject.application")
    If pjapp Is Nothing Then
        MsgBox "Project is not installed"
        Exit Sub
    End If
    pjapp.Visible = True
    Set newproj = pjapp.Projects.Add
    pjapp.NewTasksStartOn
    
    Dim rngMain As Range
    Set rngMain = ActiveWorkbook.Names("Maintasks").RefersToRange
    Dim MainTask As Range
    Dim tskPredTaskMain As Object
    
    For Each MainTask In rngMain.Cells
        Dim tskSummary As Object
        Set tskSummary = newproj.Tasks.Add(MainTask.Value)
        tskSummary.OutlineLevel = 1
    
        Dim rngMS As Range
        Set rngMS = ActiveWorkbook.Names(MainTask.Value & "_Milestones").RefersToRange
        Dim Milestone As Range
        Dim tskPredTaskMS As Object
        Set tskPredTaskMS = Nothing
    
        For Each Milestone In rngMS
            Dim tskMS As Object
            Set tskMS = newproj.Tasks.Add(Milestone.Value)
            ' use duration stored in days in column to the right
            tskMS.Duration = Milestone.Offset(, 1).Value * 8 * 60
            tskMS.OutlineLevel = 2
    
            If Not tskPredTaskMS Is Nothing Then
                tskMS.Predecessors = tskPredTaskMS.ID
            End If
            Set tskPredTaskMS = tskMS
    
        Next Milestone
    
        If Not tskPredTaskMain Is Nothing Then
            tskSummary.Predecessors = tskPredTaskMain.ID
        End If
        Set tskPredTaskMain = tskSummary
    
    Next MainTask
    
    End Sub
    

    【讨论】:

    • 亲爱的瑞秋 - 非常感谢你 - 完美无缺。我还有一些其他问题,希望您也能帮助我。我想从名为 MainTaskTime 的表中为主要任务添加持续时间。你能帮我写代码吗?
    • 由于主要任务最终为摘要任务,其持续时间由其下的里程碑任务决定;您不能在摘要任务上设置持续时间字段。也许您会想为里程碑任务设置持续时间。
    • 关于页脚,是的,不同的视图会有不同的页眉/页脚,因此您需要在使用该方法时确定哪个视图;最常用的视图是甘特图。
    • 能否添加一个基于命名范围(例如“maintask_Milestones_duration”)设置里程碑任务时间的示例?
    • @JesperKindtLarsen 代码已更新,以展示如何使用包含里程碑任务持续时间的相邻单元格。关于设置标题的错误,开始一个新问题,展示你所做的事情并提供有关错误的详细信息。
    猜你喜欢
    • 1970-01-01
    • 2015-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-23
    • 1970-01-01
    • 1970-01-01
    • 2011-02-03
    相关资源
    最近更新 更多