Name pivot table

Hidden Dan

Board Regular
Joined
Dec 7, 2016
Messages
63
I thought it was an easy job, but ...

I need to name my pivot table according a cell value. I currently type the name manually into the appropriate pivot table options field. But I presume a simple VBA can do the task better.

The reason is that when I copy a sheet a couple of times the pivot table keeps the same name.


So in cell D5 I read "Table net income" and subsequently my pivot table should get that name by executing a macro.


How can I do that best ?

Thanks, Dan
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hi,

you could use something like this but be aware this code will assume there's only 1 pivot on the copied worksheet. If there are mote you need to adress each one individual.

Code:
Sub RenamePivot()
    With Sheets("Sheet1")
        .PivotTables(1).Name = range("D5").Value
    End With
End Sub
 
Upvote 0
Joris, thanks.

There will be only one pivot table on the sheet. Actually sheets are copied once a year so sheets are named 2015 / 2016 /2017 /2018 etcetera. The year is read from a cell value and automatically the sheet is updated by grabbing data from the underlaying sheet. Therefor I need to create a simple macro that does 2 things. (1) changing pivot table name (2) updating pivot table. For the first I needed to know how to automatically change the name. Otherwise it would keep the name from the former year.

The other issues in the macro are currently present.

Dan
 
Upvote 0
Hi,

you could use something like this but be aware this code will assume there's only 1 pivot on the copied worksheet. If there are mote you need to adress each one individual.

Code:
Sub RenamePivot()
    With Sheets("Sheet1")
        .PivotTables(1).Name = range("D5").Value
    End With
End Sub

I tried code and it works for 1 defined sheet. In my example my sheet is named 2017.

Code:
Sub RenamePivot()
With Sheets("2017")
        .PivotTables(1).Name = Range("D2").Value
    End With
End Sub

But when I copy sheet 2017 and rename it to 2018, script reads D2 from 2017. So I changed script to

Code:
Sub RenamePivot()
    With ActiveSheet
        .PivotTables(1).Name = Range("D2").Value
    End With
End Sub
But this adaptation doesn't work. It still reads D2 from 2017 instead of D2 from 2018 .


I thought my adapation was correct but apparently I'm wrong. What should I do now ?


Dan
 
Last edited:
Upvote 0
Hi Hidden Dan

The code is not working as you intended because of the start of the With statement:

Code:
With ActiveSheet

The ActiveSheet is, as the name suggests, whatever sheet is visible at the time the code gets to that line. The code references the '2017' tab because that is the active sheet at the time the code is run.

Consequently, if you want your code to reference the sheet you have most recently copied, you either need to modify the code, or manually make the sheet you want the ActiveSheet.

It sounds like you are manually copying sheets; if so, then you need to make that new sheet the ActiveSheet and then run your code. You could instead codify the creation/copying of a sheet and then have the code reference that newly created/copied sheet.

Cheers

pvr928
 
Last edited:
Upvote 0
Hi Hidden Dan

The code is not working as you intended because of the start of the With statement:

Code:
With ActiveSheet

I made a horrible mistake. I stored macro in 'Sheet' section rather than 'Module' !!

Now this is the code I got now and it works well.

Code:
Sub RenamePivot_2()


    Dim currentName As String
    currentName = ActiveSheet.Name


    With Sheets(currentName)
        .PivotTables(1).Name = Range("D2").Value
    End With
End Sub


Thanks for helping.

Dan
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

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