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
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With