Renaming Pivot Table Name via VBA

VanceLiving

New Member
Joined
Mar 5, 2013
Messages
45
Hello,

Wondering if it is possible to use macro to open a Pivot Table file and rename the Pivot Table Name without initially knowing what the Pivot Table Name is? I have been generating a Pivot Table file which has a random Pivot Table Name affixed to each time. In my current process, I open the file and change the Pivot Table Name to "PivotTable1" each time before proceeding. Hoping there is a way to eliminate this step. Certainly appreciate any help - thanks!!
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Hi and Welcome to the Board,

That's easier to do if there is only one PivotTable on the specified sheet as it sounds like you have.

Code:
Sub RenameSinglePivot()
'--Assumes there is only one PivotTable on specified Sheet
    With Sheets("Sheet1")
        .PivotTables(1).Name = "PivotTable1"
    End With
End Sub

If you have more than one PivotTable on the sheet, you need to address:
1. Referencing the correct PivotTable
2. Ensuring that there is not already a different PivotTable on the same sheet with that name.
 
Upvote 0
I have dobut on this. What if you want to name a pivot table to say "abcPivotTable" while creating a pivot table and not renaming it, so the default name stays as "abcPivotTable" rather than PivotTables(1), PivotTables(2) and so on?
 
Last edited:
Upvote 0
Sub Change_PivotTable_Name()
Dim pt as pivottable
Dim wsp as worksheet
set wsp = Thisworkbook.sheets(1)
For Each pt In wsp.PivotTables
pt.Name = "PivotTable" & " " & Format(Now, "mm/dd/yy HH:mm:ss")
Next pt
End Sub
 
Upvote 0
Another approach which will rename the Pivot Table the same as the Worksheet:

VBA Code:
Sub getPVData()
Dim ws      As Worksheet
Dim pts     As PivotTables
Dim pt      As PivotTable
Dim pf      As PivotField
Dim ptRng   As Range

Set ws = ActiveSheet
Set pts = ActiveSheet.PivotTables
If pts.Count < 1 Then Exit Sub
Set pt = ws.PivotTables(1)
pt.Name = ws.Name

'* All the currently VISIBLE data in the pivot table
Set rng = pt.TableRange1
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,843
Members
449,051
Latest member
excelquestion515

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