1
votes

In MS Project (2013) can anyone show me how to copy a resource custom field (from the resource view tab) to a subproject tasks' custom field (task view tab)? Found the following to copy from an assignment field (resource usage view) to task field but I've no idea how to address a resource field...

Sub CopyAssignmentFieldToTask()
Dim t As Task
Dim ts As Tasks
Dim a As Assignment
Set ts = ActiveProject.Tasks
For Each t In ts
If Not t Is Nothing Then
t.Text5 = ""
For Each a In t.Assignments
'change the following line to use
'for a different custom field
t.Text5 = t.Text5 & ", " & a.Text5
Next a
End If
Next t
End Sub

source: http://zo-d.com/blog/archives/programming/working-with-task-and-assignment-fields-vba.html

edit: many thanks Rachel... for future ref, here's the complete answer which cycles through subprojects:

Sub CopyResourceUnitstoTasksv2()
Dim t As Task
Dim a As Assignment
Dim mProj As Project

Set mProj = ActiveProject
For Each Subproject In mProj.Subprojects
For Each t In ActiveProject.Tasks
    If Not t Is Nothing Then
        For Each a In t.Assignments
            t.Number2 = a.Resource.Number1
        Next a
    End If
Next t
Next Subproject
End Sub
1

1 Answers

0
votes

The Assignment object has a Resource property that returns the Resource object:

Sub CopyResourceFieldToTask()
    Dim t As Task
    Dim a As Assignment
    Dim t5 As String
    For Each t In ActiveProject.Tasks
        If Not t Is Nothing Then
            t5 = vbNullString
            For Each a In t.Assignments
                t5 = t5 & ", " & a.Resource.Text5
            Next a
            If Len(t5) > 2 Then
                t.Text5 = Mid(t5, 3)
            Else
                t.Text5 = vbNullString
            End If
        End If
    Next t
End Sub