Sarah Fleury
Sarah Fleury

Reputation: 1

Create VBA code to group by tasks in Ms Project according to string separated by coma

I am trying to group by task in Ms project however the functionnality already existing only groups by entire string found in the cell. I want to however classify task according to multiple string found in a cell value. It means that the task could appear in two groups because it has two strings. I want to show this view as the noraml "Group by" functionnality meaning that I can remove this categorization of task and come back to my original view.

I have manage to create a list with the group names and the task that are classified in it. However I have more problem with the display now. I miss the part to display temporarily the summary task and the task that belongs to it.

``Sub GroupTasksByString() Dim selectedColumn As String Dim task As task Dim splitValues() As String Dim i As Integer Dim uniqueGroups As Object Dim cellValue As String Dim groupName As String Dim key As Variant Dim groupSummary As task Dim taskInGroup As task

' Prompt the user to input the column name
selectedColumn = InputBox("Enter the name of the column to group by (e.g., Text1, Text2, etc.):", "Group By Column")

If selectedColumn = "" Then
    MsgBox "No column name entered. Macro will exit.", vbExclamation
    Exit Sub
End If

' Create a dictionary to hold unique groups
Set uniqueGroups = CreateObject("Scripting.Dictionary")

' Loop through all tasks in the project
For Each task In ActiveProject.Tasks
    If Not task Is Nothing Then
        ' Get the value of the cell in the selected column
        cellValue = task.GetField(FieldNameToFieldConstant(selectedColumn))
        If cellValue <> "" Then

            ' Split the value by commas
            splitValues = Split(cellValue, ",")

            ' Loop through each split value
            For i = LBound(splitValues) To UBound(splitValues)
                groupName = Trim(splitValues(i))

                ' Add the group name to the dictionary if it doesn't already exist
                If groupName <> "" Then
                    If Not uniqueGroups.exists(groupName) Then
                        uniqueGroups.Add groupName, New Collection
                    End If

                    ' Add the task to the corresponding group
                    uniqueGroups(groupName).Add task
                
                End If
            Next i
          End If
    End If
Next task

'Now, create a new grouping in the project OutlineShowAllTasks GroupClear

``

Thank you for your help.

I tried to add task but it does not replace the current task just adding them so it is not a "different view ".

Upvotes: 0

Views: 67

Answers (0)

Related Questions