使用项目宏时如何提高循环性能?

how can i improve loop performance when using project macro?

从下面的VBAProject2013的代码可以看出,当任务数超过1000时,"for"的循环大约需要50-80秒才能完成。如何才能我提高性能?有没有像excelVBA这样的"array"方法?感谢您的帮助!

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

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

让项目为外部任务、摘要和文本 1 添加列。然后用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

希望这会加快速度