Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MS Project: Loop through filtered tasks

Tags:

vba

ms-project

From a MS-Project I'd like to copy to an excel sheet the task names that meet the criteria of a filter. Let's say filter dumb tasks. I was trying something but it's not working:

Dim b As Task

For Each b In ActiveProject.TaskFilters("dumb tasks")
    'code to copy to excel
Next
like image 440
peetman Avatar asked Jul 31 '26 09:07

peetman


2 Answers

If you are using a custom field Flag3 for the filter, you can loop through the tasks, and check each one if b.Flag3 = True, and then copy this task to Excel.

Dim b As Task

For Each b In ActiveProject.Tasks
    If b.Flag3 = True Then
        ' here do your copy>>paste to Excel

    End If
Next
like image 125
Shai Rado Avatar answered Aug 04 '26 07:08

Shai Rado


To loop through only the tasks that are visible after applying a filter, select all tasks and loop through the collection of visible tasks.

Sub LoopThroughFilteredTasks()

    Dim CurrentTaskUID As Long
    CurrentTaskUID = ActiveCell.Task.UniqueID

    FilterApply "dumb tasks"
    SelectAll
    Dim FilteredTasks As Tasks
    Set FilteredTasks = ActiveSelection.Tasks
    Dim tsk As Task
    For Each tsk In FilteredTasks
        ' do something
    Next tsk
    FilterApply "&All Tasks"

    Application.Find "Unique ID", "equals", CurrentTaskUID

End Sub

Note 1: While not necessary, users generally appreciate the active selection being restored at the end of the macro, thus the CurrentTaskUID lines.

Note 2: Since filters can be complex, it is preferable to use the actual filter rather than try to replicate it in code.

like image 45
Rachel Hettinger Avatar answered Aug 04 '26 07:08

Rachel Hettinger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!