I am trying to use the following code...
Sub CreatePivotTable()
Dim WSD As Worksheet
Dim PTCache As PivotCache
Dim PVT As PivotTable
Dim PRange As Range
Dim FinalRow As Long
Dim FinalCol As Long
Dim PVTRows As Long
Dim PVTCols As Long
Dim NewRow As Long
Dim WS As Worksheet
Application.ScreenUpdating = False
Set WSD = Worksheets("StratixTimeReport")
Set WS = Worksheets("Report Summary")
For Each PVT In WSD.PivotTables
PVT.TableRange2.Clear
Next PVT
FinalRow = WSD.Cells(Application.Rows.Count, 1).End(xlUp).Row
FinalCol = WSD.Cells(1, Application.Columns.Count).End(xlToLeft).Column
Set PRange = WSD.Cells(1, 1).Resize(FinalRow, FinalCol)
Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=PRange.Address)
Set PVT = PTCache.CreatePivotTable(TableDestination:=WS.Cells(1, 1), TableName:="PivotTable1")
With PVT.PivotFields("Tech")
.Orientation = xlRowField
.Position = 1
End With
With PVT.PivotFields("Product")
.Orientation = xlRowField
.Position = 2
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Total Batch Items"), "Sum of Total Batch Items", _
xlSum
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Elapsed Time"), "Sum of Elapsed Time", xlSum
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Expected Time"), "Sum of Expected Time", xlSum
ActiveSheet.PivotTables("PivotTable1").CalculatedFields.Add _
"Rate of Productivity", "='Expected Time' /'Elapsed Time'", True
ActiveSheet.PivotTables("PivotTable1").PivotFields("Rate of Productivity"). _
Orientation = xlDataField
With ActiveSheet.PivotTables("PivotTable1").DataPivotField
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields( _
"Sum of Rate of Productivity")
.NumberFormat = "0.00%"
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields( _
"Sum of Elapsed Time")
.NumberFormat = "0.00"
End With
With PVT
.ManualUpdate = True
.ShowTableStyleColumnStripes = True
.ShowTableStyleRowStripes = True
.TableStyle2 = "PivotStyleMedium6"
.ColumnGrand = False
.RowGrand = True
.RowAxisLayout xlCompactRow
.ManualUpdate = False
End With
ActiveSheet.PivotTables("PivotTable1").DataPivotField.PivotItems( _
"Sum of Rate of Productivity").Caption = "Rate of Productivity"
ActiveSheet.PivotTables("PivotTable1").DataPivotField.PivotItems( _
"Sum of Expected Time").Caption = "Total Expected Time"
ActiveSheet.PivotTables("PivotTable1").PivotSelect "'Sum of Elapsed Time'", _
xlDataAndLabel, True
ActiveSheet.PivotTables("PivotTable1").DataPivotField.PivotItems( _
"Sum of Elapsed Time").Caption = "Total Elapsed Time"
ActiveSheet.PivotTables("PivotTable1").DataPivotField.PivotItems( _
"Sum of Total Batch Items").Caption = "Total Batch Size"
ActiveWorkbook.ShowPivotTableFieldList = False
Application.ScreenUpdating = True
End Sub
I keep getting an error relating to PivotTable Properties when the codes gets to this portion of the code...
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Total Batch Items"), "Sum of Total Batch Items", _
xlSum
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Elapsed Time"), "Sum of Elapsed Time", xlSum
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Expected Time"), "Sum of Expected Time", xlSum
ActiveSheet.PivotTables("PivotTable1").CalculatedFields.Add _
"Rate of Productivity", "='Expected Time' /'Elapsed Time'", True
ActiveSheet.PivotTables("PivotTable1").PivotFields("Rate of Productivity"). _
Orientation = xlDataField
And I am going nuts trying to resolve. Can anyone please give me some insight or assistance to resolve this issue?
Bookmarks