vbams-projectvba7vba6

How to filter tasks in MS Project, using VBA, and with multiple conditions?


I have a line of VBA code that creates a filter with one condition, this works well:

FilterEdit Name:="temp", TaskFilter:=True, Create:=True, OverwriteExisting:=True, FieldName:="Resource Names", Test:="contains", Value:='Electician', ShowInMenu:=False, ShowSummaryTasks:=False

The equivalent popup window would be:

Filter Definition Popup for One Condition

1

However I want to filter for rows containing that string AND have an outline level greater than 2.

The filter popup for this is:

Filter Definition Popup for Two Conditions

2

The filter popup would work well if I only needed it once, however the conditions I use change dynamically so I need to define it using VBA.

The Microsoft Documentation for FilterEdit mentions the Operation argument but there is no example of how to use it.

I tried stringing together several arguments in the function but it did not work:

FilterEdit Name:="temp", TaskFilter:=True, Create:=True, OverwriteExisting:=True, FieldName:="Resource Names", Test:="contains", Value:='Electician', Operation:="And", FieldName:="Outline Level", Test:="is less than or equal to", Value:=2, ShowInMenu:=False, ShowSummaryTasks:=False

Solution

  • Creating multiple criteria in a filter requires additional calls to the FilterEdit method as such:

    Sub MakeFilter()
        
        FilterEdit Name:="temp", TaskFilter:=True, Create:=True, OverwriteExisting:=True _
            , FieldName:="Resource Names", Test:="contains", Value:="Electrician" _
            , ShowInMenu:=False, ShowSummaryTasks:=False
            
        FilterEdit Name:="temp", TaskFilter:=True, FieldName:="", NewFieldName:="Outline Level" _
            , Test:="is less than or equal to", Value:="2", Operation:="And"
    
    End Sub
    

    Note that Value is always a string and that optional arguments do not always need to be included if the default value is desired (see documentation in link above).