anthonyexcel
Active Member
- Joined
- Jun 10, 2011
- Messages
- 258
- Office Version
- 365
- Platform
- Windows
Can someone please help me. The macro below works fine except the colored lines below. What I would like to be able to do is everytime I run this pivot table macro it places the pivot table on a new sheet. If the sheet exists make a new one. Thanks in advance!
Rich (BB code):
Sub Macro1()
Application.ScreenUpdating = False
Worksheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Report!R1C1:R34C27", Version:=xlPivotTableVersion10).CreatePivotTable _
TableDestination:="Sheet5!R3C1", TableName:="PivotTable1", DefaultVersion _
:=xlPivotTableVersion10
Sheets("Sheet5").Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Group")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Year")
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Total Mathematics Number Tested"), _
"Sum of Total Mathematics Number Tested", xlSum
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Total Mathematics % Lvl 1"), _
"Sum of Total Mathematics % Lvl 1", xlSum
With ActiveSheet.PivotTables("PivotTable1").DataPivotField
.Orientation = xlColumnField
.Position = 2
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Total Mathematics % Lvl 2"), _
"Sum of Total Mathematics % Lvl 2", xlSum
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Total Mathematics % Lvl 3"), _
"Sum of Total Mathematics % Lvl 3", xlSum
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Total Mathematics % Lvl 4"), _
"Sum of Total Mathematics % Lvl 4", xlSum
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Total Mathematics % Lvl 5"), _
"Sum of Total Mathematics % Lvl 5", xlSum
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Total Mathematics % Goal Range"), _
"Sum of Total Mathematics % Goal Range", xlSum
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Total Mathematics % Proficient"), _
"Sum of Total Mathematics % Proficient", xlSum
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Total Reading Total Reading"), _
"Sum of Total Reading Total Reading", xlSum
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Total Reading % Lvl 1"), _
"Sum of Total Reading % Lvl 1", xlSum
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Total Reading % Lvl 2"), _
"Sum of Total Reading % Lvl 2", xlSum
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Total Reading % Lvl 3"), _
"Sum of Total Reading % Lvl 3", xlSum
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Total Reading % Lvl 4"), _
"Sum of Total Reading % Lvl 4", xlSum
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Total Reading % Lvl 5"), _
"Sum of Total Reading % Lvl 5", xlSum
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Total Reading % Goal Range"), _
"Sum of Total Reading % Goal Range", xlSum
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Total Reading % Proficient"), _
"Sum of Total Reading % Proficient", xlSum
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Total Writing Number Tested"), _
"Sum of Total Writing Number Tested", xlSum
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Total Writing % Lvl 1"), _
"Sum of Total Writing % Lvl 1", xlSum
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Total Writing % Lvl 2"), _
"Sum of Total Writing % Lvl 2", xlSum
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Total Writing % Lvl 3"), _
"Sum of Total Writing % Lvl 3", xlSum
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Total Writing % Lvl 4"), _
"Sum of Total Writing % Lvl 4", xlSum
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Total Writing % Lvl 5"), _
"Sum of Total Writing % Lvl 5", xlSum
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Total Writing % Goal Range"), _
"Sum of Total Writing % Goal Range", xlSum
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Total Writing % Proficient"), _
"Sum of Total Writing % Proficient", xlSum
With ActiveSheet.PivotTables("PivotTable1").DataPivotField
.Orientation = xlColumnField
.Position = 1
End With
Application.ScreenUpdating = True
End Sub