3

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
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
peetman
  • 669
  • 2
  • 15
  • 30

3 Answers3

2

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
Shai Rado
  • 33,032
  • 6
  • 29
  • 51
1

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.

Rachel Hettinger
  • 7,927
  • 2
  • 21
  • 31
-1

Could it be that you just need to adjust the line

Dim b As Task

to

Dim b As Filter

According to the docs TaskFilters returns a collection of Filters

I hope this solves the issue. If you need more help with your current problem, please ask another question or edit your current one to clarify where you need help.

E. Villiger
  • 876
  • 10
  • 27