【问题标题】:Return the value in the task above the active cell in MS Project using VBA使用 VBA 返回 MS Project 中活动单元格上方任务中的值
【发布时间】:2018-10-18 13:06:21
【问题描述】:

我对 Excel 中的 VBA 很熟悉,但在我的一生中,我不知道如何使用 VBA 返回高于 MS Project 中活动单元格的值。在 Excel 中,我只会使用类似 activecell.offset(-1,0).value 的东西。

在下面的代码中,我(错误地)使用了 OFFSET,因此需要一些可以替换该代码的东西才能使我的宏工作。

当 Text4 列中的值针对下一个任务发生更改(否则缩进)时,代码会尝试添加新的任务摘要。

提前谢谢你!

Sub Add_Task_Summaries()
'Add a Summary Task when the text in the Learning Path column changes and 
indent otherwise
Dim T As Task
Dim LP As String
Dim RowNo As Long
Dim TU As Integer

For Each T In ActiveProject.Tasks

If T.Text4 <> "" And T.Summary = False Then
    If T.Text4 = T.Text4.Offset(-1, 0) Then 'INCORRECT SYNTAX
        T.OutlineIndent
    ElseIf T.Text4 <> T.Text4.Offset(-1, 0) Then 'INCORRECT SYNTAX
        LP = T.Text4
        T.Add Name:=LP, before:=ActiveSelection.Tasks
    End If
End If
Next T

End Sub

【问题讨论】:

    标签: vba ms-project


    【解决方案1】:

    您可以使用 Application 对象的各种 Select 方法(例如 SelectCellDownSelectCellRightSelectBeginning)在 MS Project 中选择单元格。但是你也可以使用这样的方法:

    Sub Add_Task_Summaries()
        'Add a Summary Task when the text in the Learning Path column changes
        'and indent otherwise
    
        Dim T As Task
        Dim S As Task
        Dim LP As String
    
        For Each T In ActiveProject.Tasks
            If T.Text4 > "" And Not T.Summary Then
                If LP <> T.Text4 Then
                    LP = T.Text4
                    Set S = ActiveProject.Tasks.Add(LP & " - Summary", T.ID)
                    S.OutlineLevel = T.OutlineLevel
                End If
                T.OutlineIndent
            End If
        Next T
    
    End Sub
    

    【讨论】:

    • 每当循环执行一组项目任务时,我建议检查并跳过空白任务(否则会导致代码崩溃)。测试一个空白任务(在“For Each t In ActiveProject.Tasks”之后)如下: > If Not t Is Nothing Then > End If > 注意 - "If t is nothing" 将在空白任务上失败,您必须使用 not 运算符。
    猜你喜欢
    • 1970-01-01
    • 2015-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-01
    • 1970-01-01
    • 2013-03-13
    相关资源
    最近更新 更多