Very new to VBA programming. I created a Macro in MS Project 2010 that creates a data file in Excel and then turns it into a Pivot Table. The code executes fine on the initail run but fails to generate the pivot table again unless I close MS Project and rerun.
I suspect the issue has to do with the following line of code
ActiveWorkbook.Names.Add Name:="PvtData", RefersToR1C1:= _
"=OFFSET(Sheet1!R1C1,0,0,COUNTA(Sheet1!C1),COUNTA(Sheet1!R1))"
where I am referencing 'Sheet1' but I can't figure a way around it to get the same results. I've tried a dozen other methods and none seem to work. Any help would be greatly appreciated. Thanks.
Dim pj As Project
Dim PjRes As Resources
Dim xlApp As Excel.Application
Dim IdSheet As Integer
Dim XlSheet As Excel.Worksheet
Dim tsvs As TimeScaleValues
Dim tsv As TimeScaleValue
Dim TSVaval As TimeScaleValues
Dim TSVmon As TimeScaleValues
Dim A As Assignment
Dim X As Resource
Dim Tc As Long
Dim Tsk As String
Dim Tsk2 As String
Dim RscG As String
Dim WorkH As Long
Dim St As Date
Dim st1 As Date
Dim WorkH1 As Long
Set pj = ActiveProject
Set PjRes = pj.Resources
Set xlApp = New Excel.Application
xlApp.Visible = True
Set xlbook = xlApp.Workbooks.Add
xlbook.Title = pj.Title
Set XlSheet = xlbook.Worksheets("Sheet1")
Dim XlSheet2 As Excel.Worksheet
Set XlSheet2 = xlbook.Worksheets("Sheet2")
If pj.Resources.Count > 0 Then
XlSheet.Cells(1, 1) = "Group"
XlSheet.Cells(1, 2) = "Resource"
XlSheet.Cells(1, 3) = "Date"
XlSheet.Cells(1, 4) = "Percent Utilized"
Row = 2
'Loop through each resource in the active project
For Each X In ActiveProject.Resources
If Not (X Is Nothing) Then
RscG = X.Group
'Check to see if the resource is a resource request
If X.Flag1 = "True" Then
Req = "Yes"
Else
Req = "No"
End If
'Loop through each assignment and calculate work values
For Each A In X.Assignments
If Not (A Is Nothing) Then
'Set tsvs = A.TimeScaleData(StartDate:=Start, EndDate:=Finish, Type:=pjAssignmentTimescaledWork, TimescaleUnit:=pjTimescaleMonths)
Set tsvs = A.TimeScaleData(StartDate:=Start, EndDate:=Finish, Type:=pjAssignmentTimescaledPercentAllocation, TimescaleUnit:=pjTimescaleMonths)
'Set TSVaval = X.TimeScaleData(Start, Finish, Type:=pjResourceTimescaledRemainingAvailability, TimescaleUnit:=pjTimescaleMonths)
Set TSVmon = X.TimeScaleData(Start, Finish, Type:=pjResourceTimescaledWorkAvailability, TimescaleUnit:=pjTimescaleMonths)
WorkH = 0
WorkH1 = 0
st1 = tsvs(1).StartDate
For T = 1 To tsvs.Count
If Not tsvs(T) = "" Then
XlSheet.Cells(Row, 1) = RscG
XlSheet.Cells(Row, 2) = A.ResourceName
XlSheet.Cells(Row, 3) = tsvs(T).StartDate
XlSheet.Cells(Row, 4) = Format(tsvs(T) / 100, "0.00%")
Row = Row + 1
End If
Next T
End If
Next A
End If
Next X
End If
ActiveWorkbook.Names.Add Name:="PvtData", RefersToR1C1:= _
"=OFFSET(Sheet1!R1C1,0,0,COUNTA(Sheet1!C1),COUNTA(Sheet1!R1))"
XlSheet2.Select
Cells.Select
Selection.Clear
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, _
SourceData:="PvtData").CreatePivotTable _
TableDestination:="Sheet2!R3C1", _
TableName:="PivotTable2"
ActiveSheet.Cells(3, 1).Select
ActiveSheet.PivotTables("PivotTable2").ColumnGrand = False
ActiveSheet.PivotTables("PivotTable2").AddFields RowFields:="Resource"
'ActiveSheet.PivotTables("PivotTable2").PivotFields("Percent Utilized").Orientation = _
xlDataField
ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables("PivotTable2").PivotFields("Percent Utilized"), "Sum of Percent Utilized", xlSum
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Date")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Group")
.Orientation = xlColumn
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable2").NullString = "0"
With ActiveSheet.PivotTables("PivotTable2").PivotFields( _
"Sum of Percent Utilized")
.NumberFormat = "0.00%"
End With
Bookmarks