Hello,
I am trying to create a macro that creates pivot tables on one sheet. On my first table I set filters to its appropriate values and then I copy that pivot table and insert it a few rows under and this second pivot will have the same format but different filters. My goal is to have 5 pivot tables into one sheet, but I believe my errors occur because of the Pivot Table name. I've highlighted the code that gives me the error in red[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=000]#000[/URL] 000][/COLOR][COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=000]#000[/URL] 000]. Is there a simple solution to this so I can run the macro on any excel file that has the same format but different data?[/COLOR]
Here is some of my code:
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Sheet1!R1C1:R221674C115", Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:="" & wsNew.Name & "!R3C1", TableName:="PivotTable1", DefaultVersion _
:=xlPivotTableVersion14
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("BUILDING_LOCID")
.Orientation = xlPageField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("OPTION_CODE")
.Orientation = xlPageField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("OPTION_STATUS_NAME")
.Orientation = xlPageField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("LINE_ITEM_TYPE")
.Orientation = xlPageField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("LINEITEM")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("C_YEAR")
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("ACTUALS_RO"), "Sum of ACTUALS_RO", xlSum
ActiveSheet.PivotTables("PivotTable1").PivotFields("OPTION_CODE").CurrentPage _
= "(All)"
With ActiveSheet.PivotTables("PivotTable1").PivotFields("OPTION_CODE")
.PivotItems("[NULL]").Visible = False
.PivotItems("A").Visible = False
.PivotItems("B").Visible = False
.PivotItems("C").Visible = False
.PivotItems("D").Visible = False
.PivotItems("E").Visible = False
.PivotItems("F").Visible = False
.PivotItems("P").Visible = False
.PivotItems("R").Visible = False
End With
ActiveSheet.PivotTables("PivotTable1").PivotFields("OPTION_CODE"). _
EnableMultiplePageItems = True
ActiveSheet.PivotTables("PivotTable1").PivotFields("OPTION_STATUS_NAME"). _
ClearAllFilters
ActiveSheet.PivotTables("PivotTable1").PivotFields("OPTION_STATUS_NAME"). _
CurrentPage = "Forecast"
ActiveSheet.PivotTables("PivotTable1").PivotFields("LINE_ITEM_TYPE"). _
ClearAllFilters
ActiveSheet.PivotTables("PivotTable1").PivotFields("LINE_ITEM_TYPE"). _
CurrentPage = "P&L"
With ActiveSheet.PivotTables("PivotTable1").PivotFields("LINEITEM")
.PivotItems("CS Total - P&L").Visible = False
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Sum of ACTUALS_RO")
.NumberFormat = "$#,##0.00"
End With
ActiveSheet.PivotTables("PivotTable1").TableStyle2 = "PivotStyleMedium9"
ActiveSheet.PivotTables("PivotTable1").Name = "Pivot1"
Range("A1:H15").Select
Selection.Copy
ActiveWindow.SmallScroll Down:=12
Range("A26").Select
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveWindow.SmallScroll Down:=9
ActiveWorkbook.ShowPivotTableFieldList = False
ActiveWorkbook.ShowPivotTableFieldList = True
Range("A26").Select
ActiveSheet.PivotTables("PivotTable2").Name = "Pivot2"
ActiveSheet.PivotTables("Pivot2").PivotFields("OPTION_CODE").CurrentPage = _
"(All)"
With ActiveSheet.PivotTables("Pivot2").PivotFields("OPTION_CODE")
.PivotItems("AUTO RENEWAL").Visible = False
.PivotItems("RO").Visible = False
End With
I am trying to create a macro that creates pivot tables on one sheet. On my first table I set filters to its appropriate values and then I copy that pivot table and insert it a few rows under and this second pivot will have the same format but different filters. My goal is to have 5 pivot tables into one sheet, but I believe my errors occur because of the Pivot Table name. I've highlighted the code that gives me the error in red[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=000]#000[/URL] 000][/COLOR][COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=000]#000[/URL] 000]. Is there a simple solution to this so I can run the macro on any excel file that has the same format but different data?[/COLOR]
Here is some of my code:
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Sheet1!R1C1:R221674C115", Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:="" & wsNew.Name & "!R3C1", TableName:="PivotTable1", DefaultVersion _
:=xlPivotTableVersion14
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("BUILDING_LOCID")
.Orientation = xlPageField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("OPTION_CODE")
.Orientation = xlPageField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("OPTION_STATUS_NAME")
.Orientation = xlPageField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("LINE_ITEM_TYPE")
.Orientation = xlPageField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("LINEITEM")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("C_YEAR")
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("ACTUALS_RO"), "Sum of ACTUALS_RO", xlSum
ActiveSheet.PivotTables("PivotTable1").PivotFields("OPTION_CODE").CurrentPage _
= "(All)"
With ActiveSheet.PivotTables("PivotTable1").PivotFields("OPTION_CODE")
.PivotItems("[NULL]").Visible = False
.PivotItems("A").Visible = False
.PivotItems("B").Visible = False
.PivotItems("C").Visible = False
.PivotItems("D").Visible = False
.PivotItems("E").Visible = False
.PivotItems("F").Visible = False
.PivotItems("P").Visible = False
.PivotItems("R").Visible = False
End With
ActiveSheet.PivotTables("PivotTable1").PivotFields("OPTION_CODE"). _
EnableMultiplePageItems = True
ActiveSheet.PivotTables("PivotTable1").PivotFields("OPTION_STATUS_NAME"). _
ClearAllFilters
ActiveSheet.PivotTables("PivotTable1").PivotFields("OPTION_STATUS_NAME"). _
CurrentPage = "Forecast"
ActiveSheet.PivotTables("PivotTable1").PivotFields("LINE_ITEM_TYPE"). _
ClearAllFilters
ActiveSheet.PivotTables("PivotTable1").PivotFields("LINE_ITEM_TYPE"). _
CurrentPage = "P&L"
With ActiveSheet.PivotTables("PivotTable1").PivotFields("LINEITEM")
.PivotItems("CS Total - P&L").Visible = False
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Sum of ACTUALS_RO")
.NumberFormat = "$#,##0.00"
End With
ActiveSheet.PivotTables("PivotTable1").TableStyle2 = "PivotStyleMedium9"
ActiveSheet.PivotTables("PivotTable1").Name = "Pivot1"
Range("A1:H15").Select
Selection.Copy
ActiveWindow.SmallScroll Down:=12
Range("A26").Select
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveWindow.SmallScroll Down:=9
ActiveWorkbook.ShowPivotTableFieldList = False
ActiveWorkbook.ShowPivotTableFieldList = True
Range("A26").Select
ActiveSheet.PivotTables("PivotTable2").Name = "Pivot2"
ActiveSheet.PivotTables("Pivot2").PivotFields("OPTION_CODE").CurrentPage = _
"(All)"
With ActiveSheet.PivotTables("Pivot2").PivotFields("OPTION_CODE")
.PivotItems("AUTO RENEWAL").Visible = False
.PivotItems("RO").Visible = False
End With