如何在使用项目宏时改善循环性能?

时间:2016-06-05 15:04:10

标签: vba macros ms-project

正如您可以从Project2013的以下VBA代码中读到的那样,当for任务数量达到1000以上时,“for”循环大约需要50-80秒。如何提高性能?是否有像excel VBA这样的“数组”方法?谢谢你的帮助!

Sub Change_Color_By_Task_Status()


' Expand all sub tasks
    SelectSheet
    OutlineShowAllTasks
    SelectTaskField Row:=1, Column:="Name"

' Clear all fields color
    SelectSheet
    FontEx CellColor:=16
    SelectTaskField Row:=1, Column:="Name", RowRelative:=False


Dim tskt As Task
For Each tskt In ActiveProject.Tasks
If Len(tskt.Name) > 0 Then
  If Not tskt Is Nothing Then  
  If Not tskt.ExternalTask Then
    If Not tskt.Summary Then    
        Select Case tskt.Text1
            Case "Complete"
                SelectRow Row:=tskt.ID, RowRelative:=False
                'Font Color:=pjBlack
                FontEx CellColor:=pjGray
            Case "Yellow"
                SelectRow Row:=tskt.ID, RowRelative:=False
                'Font Color:=pjBlack
                FontEx CellColor:=pjYellow
            Case "Green"
                SelectRow Row:=tskt.ID, RowRelative:=False
                'Font Color:=pjBlack
                FontEx CellColor:=pjWhite
            Case "Red"
                SelectRow Row:=tskt.ID, RowRelative:=False
               'Font Color:=pjRed
               FontEx CellColor:=pjRed
            Case "Overdue"
                SelectRow Row:=tskt.ID, RowRelative:=False
               Font Color:=pjWhite
               Font32Ex CellColor:=192
        End Select
     End If 
     End If  
    End If  
    End If
Next tskt

End Sub

1 个答案:

答案 0 :(得分:0)

我发现在使用界面时使用ms项目内置过滤器要快得多。

让项目为外部任务,摘要和Text1添加列。然后使用Application.SetAutoFilter过滤掉Summary = yes,ExternalTak = Yes,然后过滤掉每个Text1,SelectAll并设置格式。像这样:

Sub Change_Color_By_Task_Status()

    'Add columns to filter
    TableEditEx Name:="&Entry", TaskTable:=True, NewName:="", NewFieldName:="Text1", ColumnPosition:=0
    TableApply Name:="&Entry"
    TableEditEx Name:="&Entry", TaskTable:=True, NewName:="", NewFieldName:="External Task", Title:="", ColumnPosition:=0
    TableApply Name:="&Entry"
    TableEditEx Name:="&Entry", TaskTable:=True, NewName:="", NewFieldName:="Summary", Title:="", ColumnPosition:=0
    TableApply Name:="&Entry"

    'Filter out summaries and externals
    SetAutoFilter FieldName:="External Task", FilterType:=pjAutoFilterFlagNo
    SetAutoFilter FieldName:="Summary", FilterType:=pjAutoFilterFlagNo

    'Filter by Text1
    'for "Complete"
    SetAutoFilter FieldName:="Text1", FilterType:=pjAutoFilterCustom,     Test1:="equals", criteria1:="Complete"
    SelectAll
    '[Apply complete formatting]
    SetAutoFilter FieldName:="Text1", FilterType:=pjAutoFilterClear

    '... repeat for the other Text1 values

    'clear filters
    SetAutoFilter FieldName:="External Task", FilterType:=pjAutoFilterClear
    SetAutoFilter FieldName:="Summary", FilterType:=pjAutoFilterClear

    'Remove columns
    SelectTaskColumn Column:="Text1"
    ColumnDelete
    SelectTaskColumn Column:="Summary"
    ColumnDelete
    SelectTaskColumn Column:="External Task"
    ColumnDelete
End Sub

希望这能加快速度