Hello everyone! Long time reader first time poster here...
I have a macro that pulls data from a table into a pivot table and then sorts it. I use the same process/code 4 times so that I can break up the data into 4 separate pivot tables.
The code I use for the first 2 pivot tables works fine but for the third, I get the run time error 1004 "Unable to get the PivotFields property of the PivotTable class". I've compared the code and everything is seemingly the same. I'm at a loss for why I am getting this error.
Thank you in advance for your assistance!
In this example, everything works as planned:
Sheets("Sheet1").Select
Sheets("Sheet1").Name = "D - RCO ATT_DID"
Sheets.Add After:=ActiveSheet
Sheets.Add After:=ActiveSheet
Sheets("Sheet2").Select
Sheets("Sheet2").Name = "PT1"
Sheets("Sheet3").Select
Sheets("Sheet3").Name = "BHT1"
Sheets("rec").Select
ActiveSheet.Range("$A:$L").AutoFilter Field:=8, Criteria1:="=701", _
Operator:=xlOr, Criteria2:="=706"
Range("A:A,C:C,E:E").Select
Range("E1").Activate
Selection.Copy
Sheets("D - RCO ATT_DID").Select
ActiveSheet.Paste
Range("D1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "CCS"
Range("E1").Select
ActiveCell.FormulaR1C1 = "CALL COUNT"
Range("D2").Select
ActiveCell.FormulaR1C1 = "=RC[-1]/100"
Range("E2").Select
ActiveCell.FormulaR1C1 = "1"
Range("D2:E2").Select
Selection.NumberFormat = "0.00"
Range("E2").Select
Selection.NumberFormat = "0.0"
Selection.NumberFormat = "0"
Selection.AutoFill Destination:=Range("E2:E" & Range("A" & Rows.Count).End(xlUp).Row)
Range("E:E").Select
Range("D2").Select
Selection.AutoFill Destination:=Range("D2:D" & Range("A" & Rows.Count).End(xlUp).Row)
Range("D:D").Select
Columns("A:E").Select
ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A:$E"), , xlYes).Name = _
"Table1"
Range("F1").Select
Sheets("PT1").Select
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"TABLE1", Version:=xlPivotTableVersion15).CreatePivotTable TableDestination _
:="PT1!R1C1", TableName:="PivotTable1", DefaultVersion:= _
xlPivotTableVersion15
Sheets("PT1").Select
Cells(1, 1).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("DATE")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("START HOUR")
.Orientation = xlRowField
.Position = 2
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("CALL COUNT"), "Count of CALL COUNT", xlCount
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("SECONDS"), "Count of SECONDS", xlCount
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("CCS"), "Count of CCS", xlCount
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Count of CALL COUNT")
.Caption = "Sum of CALL COUNT"
.Function = xlSum
End With
However, in this example, I get the run-time error (where text is in red).
Sheets("Sheet7").Select
Sheets("Sheet7").Name = "F - STP ATT_DID"
Sheets("Sheet8").Select
Sheets("Sheet8").Name = "PT3"
Sheets("Sheet9").Select
Sheets("Sheet9").Name = "BHT3"
Sheets("rec").Select
Range("A1").Select
ActiveSheet.ShowAllData
ActiveSheet.Range("A:L").AutoFilter Field:=8, Criteria1:=Array("701", "703", "705"), Operator:=xlFilterValues
ActiveSheet.Range("A:L").AutoFilter Field:=11, Criteria1:="spf"
Range("A:A,C:C,E:E").Select
Range("E1").Activate
Selection.Copy
Sheets("F - STP ATT_DID").Select
ActiveSheet.Paste
Range("D1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "CCS"
Range("E1").Select
ActiveCell.FormulaR1C1 = "CALL COUNT"
Range("D2").Select
ActiveCell.FormulaR1C1 = "=RC[-1]/100"
Range("E2").Select
ActiveCell.FormulaR1C1 = "1"
Range("D2:E2").Select
Selection.NumberFormat = "0.00"
Range("E2").Select
Selection.NumberFormat = "0.0"
Selection.NumberFormat = "0"
Selection.AutoFill Destination:=Range("E2:E" & Range("A" & Rows.Count).End(xlUp).Row)
Range("E:E").Select
Range("D2").Select
Selection.AutoFill Destination:=Range("D2:D" & Range("A" & Rows.Count).End(xlUp).Row)
Range("D:D").Select
Columns("A:E").Select
ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A:$E"), , xlYes).Name = _
"Table3"
Range("F1").Select
Sheets("PT3").Select
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"TABLE3", Version:=xlPivotTableVersion15).CreatePivotTable TableDestination _
:="PT3!R1C1", TableName:="PivotTable3", DefaultVersion:= _
xlPivotTableVersion15
Sheets("PT3").Select
Cells(1, 1).Select
With ActiveSheet.PivotTables("PivotTable3").PivotFields("DATE")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable3").PivotFields("START HOUR")
.Orientation = xlRowField
.Position = 2
End With
ActiveSheet.PivotTables("PivotTable3").AddDataField ActiveSheet.PivotTables( _
"PivotTable3").PivotFields("CALL COUNT"), "Count of CALL COUNT", xlCount
ActiveSheet.PivotTables("PivotTable3").AddDataField ActiveSheet.PivotTables( _
"PivotTable3").PivotFields("SECONDS"), "Count of SECONDS", xlCount
ActiveSheet.PivotTables("PivotTable3").AddDataField ActiveSheet.PivotTables( _
"PivotTable3").PivotFields("CCS"), "Count of CCS", xlCount
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Count of CALL COUNT")
.Caption = "Sum of CALL COUNT"
.Function = xlSum
End With
The table that is sourcing the PivotTable has 5 columns (A:E). However, once the macro stops running due to the error, the table has incorrect labels and I do not know why. D1 should be "CCS" but instead it says "#VALUE!". E1 should be "CALL COUNT" but instead it says "1".
I have a macro that pulls data from a table into a pivot table and then sorts it. I use the same process/code 4 times so that I can break up the data into 4 separate pivot tables.
The code I use for the first 2 pivot tables works fine but for the third, I get the run time error 1004 "Unable to get the PivotFields property of the PivotTable class". I've compared the code and everything is seemingly the same. I'm at a loss for why I am getting this error.
Thank you in advance for your assistance!
In this example, everything works as planned:
Sheets("Sheet1").Select
Sheets("Sheet1").Name = "D - RCO ATT_DID"
Sheets.Add After:=ActiveSheet
Sheets.Add After:=ActiveSheet
Sheets("Sheet2").Select
Sheets("Sheet2").Name = "PT1"
Sheets("Sheet3").Select
Sheets("Sheet3").Name = "BHT1"
Sheets("rec").Select
ActiveSheet.Range("$A:$L").AutoFilter Field:=8, Criteria1:="=701", _
Operator:=xlOr, Criteria2:="=706"
Range("A:A,C:C,E:E").Select
Range("E1").Activate
Selection.Copy
Sheets("D - RCO ATT_DID").Select
ActiveSheet.Paste
Range("D1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "CCS"
Range("E1").Select
ActiveCell.FormulaR1C1 = "CALL COUNT"
Range("D2").Select
ActiveCell.FormulaR1C1 = "=RC[-1]/100"
Range("E2").Select
ActiveCell.FormulaR1C1 = "1"
Range("D2:E2").Select
Selection.NumberFormat = "0.00"
Range("E2").Select
Selection.NumberFormat = "0.0"
Selection.NumberFormat = "0"
Selection.AutoFill Destination:=Range("E2:E" & Range("A" & Rows.Count).End(xlUp).Row)
Range("E:E").Select
Range("D2").Select
Selection.AutoFill Destination:=Range("D2:D" & Range("A" & Rows.Count).End(xlUp).Row)
Range("D:D").Select
Columns("A:E").Select
ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A:$E"), , xlYes).Name = _
"Table1"
Range("F1").Select
Sheets("PT1").Select
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"TABLE1", Version:=xlPivotTableVersion15).CreatePivotTable TableDestination _
:="PT1!R1C1", TableName:="PivotTable1", DefaultVersion:= _
xlPivotTableVersion15
Sheets("PT1").Select
Cells(1, 1).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("DATE")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("START HOUR")
.Orientation = xlRowField
.Position = 2
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("CALL COUNT"), "Count of CALL COUNT", xlCount
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("SECONDS"), "Count of SECONDS", xlCount
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("CCS"), "Count of CCS", xlCount
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Count of CALL COUNT")
.Caption = "Sum of CALL COUNT"
.Function = xlSum
End With
However, in this example, I get the run-time error (where text is in red).
Sheets("Sheet7").Select
Sheets("Sheet7").Name = "F - STP ATT_DID"
Sheets("Sheet8").Select
Sheets("Sheet8").Name = "PT3"
Sheets("Sheet9").Select
Sheets("Sheet9").Name = "BHT3"
Sheets("rec").Select
Range("A1").Select
ActiveSheet.ShowAllData
ActiveSheet.Range("A:L").AutoFilter Field:=8, Criteria1:=Array("701", "703", "705"), Operator:=xlFilterValues
ActiveSheet.Range("A:L").AutoFilter Field:=11, Criteria1:="spf"
Range("A:A,C:C,E:E").Select
Range("E1").Activate
Selection.Copy
Sheets("F - STP ATT_DID").Select
ActiveSheet.Paste
Range("D1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "CCS"
Range("E1").Select
ActiveCell.FormulaR1C1 = "CALL COUNT"
Range("D2").Select
ActiveCell.FormulaR1C1 = "=RC[-1]/100"
Range("E2").Select
ActiveCell.FormulaR1C1 = "1"
Range("D2:E2").Select
Selection.NumberFormat = "0.00"
Range("E2").Select
Selection.NumberFormat = "0.0"
Selection.NumberFormat = "0"
Selection.AutoFill Destination:=Range("E2:E" & Range("A" & Rows.Count).End(xlUp).Row)
Range("E:E").Select
Range("D2").Select
Selection.AutoFill Destination:=Range("D2:D" & Range("A" & Rows.Count).End(xlUp).Row)
Range("D:D").Select
Columns("A:E").Select
ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A:$E"), , xlYes).Name = _
"Table3"
Range("F1").Select
Sheets("PT3").Select
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"TABLE3", Version:=xlPivotTableVersion15).CreatePivotTable TableDestination _
:="PT3!R1C1", TableName:="PivotTable3", DefaultVersion:= _
xlPivotTableVersion15
Sheets("PT3").Select
Cells(1, 1).Select
With ActiveSheet.PivotTables("PivotTable3").PivotFields("DATE")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable3").PivotFields("START HOUR")
.Orientation = xlRowField
.Position = 2
End With
ActiveSheet.PivotTables("PivotTable3").AddDataField ActiveSheet.PivotTables( _
"PivotTable3").PivotFields("CALL COUNT"), "Count of CALL COUNT", xlCount
ActiveSheet.PivotTables("PivotTable3").AddDataField ActiveSheet.PivotTables( _
"PivotTable3").PivotFields("SECONDS"), "Count of SECONDS", xlCount
ActiveSheet.PivotTables("PivotTable3").AddDataField ActiveSheet.PivotTables( _
"PivotTable3").PivotFields("CCS"), "Count of CCS", xlCount
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Count of CALL COUNT")
.Caption = "Sum of CALL COUNT"
.Function = xlSum
End With
The table that is sourcing the PivotTable has 5 columns (A:E). However, once the macro stops running due to the error, the table has incorrect labels and I do not know why. D1 should be "CCS" but instead it says "#VALUE!". E1 should be "CALL COUNT" but instead it says "1".