Rename PivotTable name in VBA

MPaulus

New Member
Joined
Oct 13, 2021
Messages
11
Office Version
  1. 365
Platform
  1. Windows
I have a macro that creates a new pivot table and the pivot table will then always have new name so I have renamed my pivot table to my sheet name, "Pivot", and now when I try to hide a column I am getting a run time error '1004': Unable to set the orientation property of the PivotField class.


VBA Code:
 Dim ws      As Worksheet
    Dim pts     As PivotTables
    Dim pt      As PivotTable

    Set ws = ActiveSheet
    Set pts = ActiveSheet.PivotTables
    If pts.Count < 1 Then Exit Sub
    Set pt = ws.PivotTables(1)
    pt.Name = ws.Name
    
    ActiveSheet.PivotTables("Pivot").PivotFields("Future").Orientation = xlHidden

Or would it work better to name the pivot table while I am creating it, what would that code look like?
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
You'll get that error whenever . . .

1) the pivot field doesn't exists, or

2) the pivot field is already hidden

Is either one the case?
 
Upvote 0
No, the field existed and had not been previously hidden. I have found a workaround.
 
Upvote 0
That's great, glad you were able to come up with a solution.

Cheers!
 
Upvote 0
But now I am having trouble renaming a PivotChart in a macro. I have a capacity report to run weekly that I will recreate a new pivot table and then a new chart once the data is sorted. The attached file shows the completed pivot table and the macro is where I am at in creating the table. The macro runs up to the point where I need to rename the chart so I can set the scale on the secondary axis to match the primary axis. Here is what I have written:

VBA Code:
Sub CreateChart_Test()
'

' RenamePivotTables Macro
    Dim ws      As Worksheet
    Dim pts     As PivotTables
    Dim pt      As PivotTable

    Set ws = ActiveSheet
    Set pts = ActiveSheet.PivotTables
    If pts.Count < 1 Then Exit Sub
    Set pt = ws.PivotTables(1)
    pt.Name = ws.Name
    
'   Create Chart
    ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Select
    ActiveChart.SetSourceData Source:=Range("Pivot!$J$11:$S$19")
    ActiveChart.FullSeriesCollection(1).ChartType = xlColumnClustered
    ActiveChart.FullSeriesCollection(1).AxisGroup = 1
    ActiveChart.FullSeriesCollection(2).ChartType = xlColumnClustered
    ActiveChart.FullSeriesCollection(2).AxisGroup = 1
    ActiveChart.FullSeriesCollection(3).ChartType = xlColumnClustered
    ActiveChart.FullSeriesCollection(3).AxisGroup = 1
    ActiveChart.FullSeriesCollection(4).ChartType = xlColumnClustered
    ActiveChart.FullSeriesCollection(4).AxisGroup = 1
    ActiveChart.FullSeriesCollection(5).ChartType = xlColumnClustered
    ActiveChart.FullSeriesCollection(5).AxisGroup = 1
    ActiveChart.FullSeriesCollection(6).ChartType = xlLine
    ActiveChart.FullSeriesCollection(6).AxisGroup = 1
    ActiveChart.FullSeriesCollection(7).ChartType = xlLine
    ActiveChart.FullSeriesCollection(7).AxisGroup = 1
    ActiveChart.FullSeriesCollection(8).ChartType = xlLine
    ActiveChart.FullSeriesCollection(8).AxisGroup = 1
    ActiveChart.FullSeriesCollection(9).ChartType = xlLine
    ActiveChart.FullSeriesCollection(9).AxisGroup = 1
    ActiveChart.FullSeriesCollection(6).ChartType = xlColumnClustered
    ActiveChart.FullSeriesCollection(7).AxisGroup = 2
    ActiveChart.FullSeriesCollection(8).AxisGroup = 2
    ActiveChart.FullSeriesCollection(9).AxisGroup = 2
    ActiveChart.FullSeriesCollection(7).ChartType = xlColumnStacked
    ActiveChart.FullSeriesCollection(8).ChartType = xlColumnStacked
    ActiveChart.FullSeriesCollection(9).ChartType = xlColumnStacked

'   RenameChart
    Dim cob     As ChartObjects
    Dim cht      As Chart

    Set ws = ActiveSheet
    Set cob = ActiveSheet.ChartObjects
    If cob.Count < 1 Then Exit Sub
    Set cht = ws.ChartObjects
    cht.Name = ws.Name

'   Set Secondary Axis to match same scale as Primary Axis
    Dim x As Long


    With ActiveSheet.ChartObjects(Pivot).Chart

        ActiveSheet.ChartObjects(Pivot).Activate
        ActiveChart.Axes(xlValue).MaximumScaleIsAuto = True
        ActiveChart.Axes(xlValue).MinimumScale = 0
        x = ActiveChart.Axes(xlValue).MaximumScale
        ActiveChart.Axes(xlValue, xlSecondary).MaximumScale = x

    End With
End Sub

Chart Trouble.xlsm
ABCDEFGHIJKLMNOPQRST
1
2
3
4
5
6
7
8
9
10
11Row LabelsSum of Total Late HrsSum of 10/25/2021Sum of 11/1/2021Sum of 11/8/2021Sum of 11/15/2021Sum of 11/22/2021Sum of 40 Hr CapacitySum of 50 Hr CapacitySum of 60 Hr Capacity
12FORM2266.59950.07677.26592.85393.13200.24550.69137.6725137.6725
13LASER956.84488.1487.85248.27291.04109.15213.0453.2653.26
14LASER UNLOAD1299.87568.36486.44308.59316.94112.05611.05152.7625152.7625
15N/A4021.92889.37788.571007.71881.84522.35694.59173.6475173.6475
16PAINT165.6987.8252.0482.1922.9519.53234.958.72558.725
17TURRETS853.46277.09227.44138.33123.1988.07213.0453.2653.26
18WELD4039.591071.851354.081237.79835.97384.561024.54256.135256.135
19Grand Total13603.964332.664073.683615.732865.061435.953541.85885.4625885.4625
20
Pivot
 
Upvote 0
First of all, Set cht = ws.ChartObjects should produce an error since you're assigning ChartObjects to a variable that has been declared as Chart. So it should be . . .

Code:
Set cht = ws.ChartObjects(1).Chart
.

Then, to rename the chart, you'll need to refer to the ChartObject, not the Chart. So it should be . . .

Code:
cht.Parent.Name = ws.Name

Note, however, your macro can be re-written as follows...

VBA Code:
Sub CreateChart_Test()

    If ActiveSheet.PivotTables.Count < 1 Then Exit Sub
    
    Dim ws As Worksheet
    Set ws = ActiveSheet

'   Rename PivotTable
    Dim pt As PivotTable
    Set pt = ws.PivotTables(1)
    pt.Name = ws.Name

'   Create and rename Chart
    Dim shp As Shape
    Set shp = ws.Shapes.AddChart2(201, xlColumnClustered)
    shp.Name = ws.Name
    
    With shp.Chart
        .SetSourceData Source:=pt.TableRange2
        With .FullSeriesCollection(1)
            .ChartType = xlColumnClustered
            .AxisGroup = 1
        End With
        'etc
        '
        '
    End With

'   Set Secondary Axis to match same scale as Primary Axis
    With shp.Chart
        .Axes(xlValue).MaximumScaleIsAuto = True
        .Axes(xlValue).MinimumScale = 0
        .Axes(xlValue, xlSecondary).MaximumScale = .Axes(xlValue).MaximumScale
    End With
    
End Sub

Hope this helps!
 
Upvote 0
Solution
Yes, that was great! One thing just seems to lead to the next....my next step is to add a slicer based on the work centers from the right hand column. When I select one of the work centers from the slicer it keeps my secondary axis scale locked at the the summary level and doesn't adjust it to match the work center.
 

Attachments

  • Summary.png
    Summary.png
    48 KB · Views: 9
  • Work Center Selected.png
    Work Center Selected.png
    31 KB · Views: 9
Upvote 0
Yes, that was great! One thing just seems to lead to the next....my next step is to add a slicer based on the work centers from the right hand column. When I select one of the work centers from the slicer it keeps my secondary axis scale locked at the the summary level and doesn't adjust it to match the work center.


View attachment 49925View attachment 49926
 
Upvote 0
That's great, glad I could help.

For a new question, though, please start a new thread and ask your question there.

Cheers!
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,039
Members
449,063
Latest member
ak94

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