Names changing in Pivot Table Macro

ahdrum87

Board Regular
Joined
Dec 15, 2011
Messages
57
Hello!


I am running into an error when creating a macro to make a pivot table and sum certain columns. I am fairly certain this is due to the name changing to PivotTable2, 3, 4, etc. each time. Is there a way to make the name stagnent so when a table is created in the macro it doesn't error out the formulas? I have an example below. Thanks so much in advance!

Code:
Columns("A:D").Select
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "Data!R1C1:R1048576C4", Version:=xlPivotTableVersion14).CreatePivotTable _
        TableDestination:="Pivot!R1C1", TableName:="PivotTable5", DefaultVersion _
        :=xlPivotTableVersion14
    Sheets("Pivot").Select
    Cells(1, 1).Select
    With ActiveSheet.PivotTables("PivotTable5").PivotFields("PRIMARY NUMBER")
        .Orientation = xlRowField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("PivotTable5").PivotFields("OFF")
        .Orientation = xlRowField
        .Position = 2
    End With
    ActiveSheet.PivotTables("PivotTable5").AddDataField ActiveSheet.PivotTables( _
        "PivotTable5").PivotFields("DEBIT"), "Count of DEBIT", xlCount
    ActiveSheet.PivotTables("PivotTable5").AddDataField ActiveSheet.PivotTables( _
        "PivotTable5").PivotFields("CREDIT"), "Count of CREDIT", xlCount
    Range("B1").Select
    With ActiveSheet.PivotTables("PivotTable5").PivotFields("Count of DEBIT")
        .Caption = "Sum of DEBIT"
        .Function = xlSum
    End With
    Range("C1").Select
    With ActiveSheet.PivotTables("PivotTable5").PivotFields("Count of CREDIT")
        .Caption = "Sum of CREDIT"
        .Function = xlSum
    End With
    Sheets("Summary").Select
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Are you pivoting the same data every time (understood that data is added/removed/changed each time poss.)? Why are you creating a new pivot table every time?
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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