Search code examples
vbaexcelloopsif-statementms-project

MSProject/Excel VBA Loop through 'Flag' columns, how?


Hopefully an easy one. In Excel VBA, I'd like to be able to loop through the custom flag columns in a MSProject file I have open.

In pseudo

For i = 1 to 20
If task.flagi then ....

Ho do I write the 'flagi' in code?

Thanks,

Jon C


Solution

  • You could do something like this:

    Set task = ActiveProject.Tasks.Add("Test Task")
    Dim Index As Integer
    For Index = 1 To 20
        Dim fieldName As String
        fieldName = "Flag" & Index
    
        Dim fieldValue
        fieldValue = task.GetField(FieldNameToFieldConstant(fieldName))
        '
        ' Your code here
        '
    Next
    

    You could also use the pjField constants directly to access the fields (https://msdn.microsoft.com/EN-US/library/office/ff867782.aspx), although they are slightly harder to work with (for example they are not in the numeric order you'd expect!)