Creating a Pivot Table Macro in VBA

Mikeykt

New Member
Joined
Feb 10, 2011
Messages
47
Hi,

I'm trying to set up a macro to create a new Pivot table on a fixed sheet. So that it deletes the previous pivot table, and creates a new one on the same worksheet (as a kind of refresh).

I haven't done this for ages, so need a little guidence on why my VBA isn't working at the creating the pivot table stage.

I thought I had the code correct, but apprently not. Can anyone let me know where I went wrong?

Thanks
Mike



Sub RSPivotNum()
'
' RSPivotNum Macro
'
'
Sheets("RSPIVOT").Select
Cells.Select
Selection.Delete Shift:=xlUp
Sheets("Inactive RG by Recruit Type 2").Select
Columns("A:G").Select


ActiveWorkbook.Worksheets("RSPIVOT").PivotTables("RSPN").PivotCache. _
CreatePivotTable TableDestination:="RSPIVOT!R1C1", TableName:="PivotTable1", _
DefaultVersion:=xlPivotTableVersion12



Sheets("RSPIVOT").Select
Cells(1, 1).Select

ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh

With ActiveSheet.PivotTables("PivotTable1").PivotFields("Behaviour")
.Orientation = xlPageField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Value")
.Orientation = xlPageField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Recency")
.Orientation = xlPageField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Segment")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Recruitment Summary")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Recruitment Source")
.Orientation = xlColumnField
.Position = 2
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("No Supporters"), "Count of No Supporters", xlCount
With ActiveSheet.PivotTables("PivotTable1").PivotFields( _
"Count of No Supporters")
.Caption = "Sum of No Supporters"
.Function = xlSum
End With

ActiveSheet.PivotTables("PivotTable1").Name = "RSPN"

End Sub
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
you will need to save the file after you delete the pivot table, else Excel still thinks that pivot table is existing
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,297
Members
452,903
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top