larinda4
Board Regular
- Joined
- Nov 15, 2021
- Messages
- 73
- Office Version
- 365
- Platform
- 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:
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?
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