Pivot Table: Copy and Paste but Rename It?

larinda4

Board Regular
Joined
Nov 15, 2021
Messages
74
Office Version
  1. 365
Platform
  1. Windows
Good morning,

I have a pivot table in cells A:C, and I want to copy A:C and paste in F:H, but rename the pivot table. My only problem is when I close the book and re-open it to try running the macro again, the pivot table name is different
ex "Pivottable3" is now "pivottable6" and I get a debug error.

Is there any way to copy and paste the pivot table, but immediately rename it without having to reference "pivottable##" to do it?

Here is my code:
VBA Code:
 'Copy current PT and paste to column F.
    Columns("A:C").Select
    Selection.Copy
    Columns("F:F").Select
    ActiveSheet.Paste
    
'Update Region Filter
    ActiveSheet.PivotTables("EAPPT7").PivotFields("Region").ClearAllFilters
    ActiveSheet.PivotTables("EAPPT7").PivotFields("Region").CurrentPage = "0007"
    ActiveSheet.PivotTables("PivotTable3").PivotFields("Region").ClearAllFilters
    ActiveSheet.PivotTables("PivotTable3").PivotFields("Region").CurrentPage = _
        "0008"

Here is the code to create the PT but I know this only works if there's one pivot table in the sheet. So should I create another insert PT macro instead of trying to rename the copied pivot table?

VBA Code:
'Insert EAP pivot table

Dim dataSheet2 As Worksheet
Set dataSheet2 = Sheets("Listing")

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
               SourceData:="'" & dataSheet2.Name & "'!" & dataSheet2.Range("A1").CurrentRegion.Address(ReferenceStyle:=xlR1C1)).CreatePivotTable _
                  TableDestination:=Sheets("EAPPT").Range("A3")
    Sheets("EAP").Select
    Cells(1, 1).Select
        'Rename Pivot Table
    With Sheets("EAPPT")
        .PivotTables(1).Name = "EAPPT7"
    End With
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Hey everyone,

I found a solution.

Instead of copying and pasting the pivot table, I created another code for inserting a pivot table beside the one I already created.

Thanks!
 
Upvote 0
In case anyone wants the code I used to create the pivot table, here it is below:

VBA Code:
    With Worksheets("Listing")
        Dim lastRow As Long
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "Listing!R1C1:R" & lastRow & "C21", Version:=6).CreatePivotTable _
        TableDestination:="EAPPT!R3C6", TableName:="EAPPT8", DefaultVersion _
        :=6
    Sheets("EAPPT").Select
    Cells(1, 6).Select
    With ActiveSheet.PivotTables("EAPPT8")
 
Upvote 0
Solution

Forum statistics

Threads
1,224,537
Messages
6,179,408
Members
452,912
Latest member
alicemil

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