Code for Pivot Charts

tjgrassi

Board Regular
Joined
Jun 10, 2014
Messages
51
I have a code for a user form that works well for the most part. It is a long code. I was wondering if there is any way to turn off a current Pivot Table Field that is selected, the only way I could figure out how to do it was turn each field off at the begining of the macro, this is the code I currently have to start my macro. I could run in to problems if someone where to try to change the pivot field manually and excel puts the new category in the row lable instead of values or if it was in values and not as a sum but as "count." I would need a code 3 times this long to cover all of the basis.

On Error Resume Next
ActiveSheet.PivotTables("PivotTable7").PivotFields("Sum of Haz Intensity"). _
Orientation = xlHidden
ActiveSheet.PivotTables("PivotTable7").PivotFields("Sum of Non-Haz Intensity"). _
Orientation = xlHidden
ActiveSheet.PivotTables("PivotTable7").PivotFields("Sum of Ink Intensity"). _
Orientation = xlHidden
ActiveSheet.PivotTables("PivotTable7").PivotFields("Sum of Water Intensity"). _
Orientation = xlHidden
ActiveSheet.PivotTables("PivotTable7").PivotFields("Sum of Gas Intensity"). _
Orientation = xlHidden
ActiveSheet.PivotTables("PivotTable7").PivotFields("Sum of Electric Intensity") _
.Orientation = xlHidden
ActiveSheet.PivotTables("PivotTable7").PivotFields("Sum of Haz (lbs)"). _
Orientation = xlHidden
ActiveSheet.PivotTables("PivotTable7").PivotFields("Sum of Non-Haz (Lbs)"). _
Orientation = xlHidden
ActiveSheet.PivotTables("PivotTable7").PivotFields("Sum of Ink (Gallons)"). _
Orientation = xlHidden
ActiveSheet.PivotTables("PivotTable7").PivotFields("Sum of Water (Gallons)"). _
Orientation = xlHidden
ActiveSheet.PivotTables("PivotTable7").PivotFields("Sum of Gas (Therms)"). _
Orientation = xlHidden
ActiveSheet.PivotTables("PivotTable7").PivotFields("Sum of Electric (kWh)"). _
Orientation = xlHidden
ActiveSheet.PivotTables("PivotTable7").PivotFields("Sum of Output (Packages)"). _
Orientation = xlHidden
ActiveSheet.PivotTables("PivotTable7").PivotFields("Sum of Output Percent Change"). _
Orientation = xlHidden
ActiveSheet.PivotTables("PivotTable7").PivotFields("Sum of Electricity Percent Change" _
).Orientation = xlHidden
ActiveSheet.PivotTables("PivotTable7").PivotFields("Sum of Gas Percent Change"). _
Orientation = xlHidden
ActiveSheet.PivotTables("PivotTable7").PivotFields("Sum of Water Percent Change"). _
Orientation = xlHidden
ActiveSheet.PivotTables("PivotTable7").PivotFields("Sum of Ink Percent Change"). _
Orientation = xlHidden
ActiveSheet.PivotTables("PivotTable7").PivotFields("Sum of Non-Haz Percent Change"). _
Orientation = xlHidden
ActiveSheet.PivotTables("PivotTable7").PivotFields("Sum of Haz Percent Change") _
.Orientation = xlHidden
 
I missed one change:

Code:
    Dim lPivotLineCount as Long ' starts with lower case l - because it is a Long variable
    lPivotLineCount = ActiveSheet.PivotTables(1).PivotRowAxis.PivotLines.Count

    ActiveSheet.PivotTables(1).PivotFields(Combobox2.Value).AutoSort _
        xlDescending, "Sum of " & Combobox2.Value, ActiveSheet.PivotTables(1).PivotRowAxis. _
        PivotLines(lPivotLineCount), 1
 
Upvote 0

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
I am now getting the error unable to get the error of unable to get the pivot fields property of the pivot table class. The error comes from this code. not sure if it would help but I am working with pivot table 7

Code:
 ActiveSheet.PivotTables(1).PivotFields(ComboBox2.Value).AutoSort _
        xlDescending, "Sum of " & ComboBox2.Value, ActiveSheet.PivotTables(1).PivotRowAxis. _
        PivotLines(lPivotLineCount), 1
 
Upvote 0
I believe that error will show if ComboBox2.Value is not an active field in your pivottable.

When the error occurs see what the value for ComboBox2.Value is.

If the value of ComboBox2 is "Bob"

Replace the code with:
Code:
ActiveSheet.PivotTables(1).PivotFields("Bob").AutoSort _
        xlDescending, "Sum of " & "Bob", ActiveSheet.PivotTables(1).PivotRowAxis. _
        PivotLines(lPivotLineCount), 1

and see if you get the same error.

If PivotTable7 is the only PivotTable on the ActiveSheet then PivotTables(1) is equivalent to PivotTables("PivotTable7")
 
Upvote 0
When I put in to the code "central" for "bob" which is the one i tried to sort. I got an error on xlDescending saying invalid use of property.
 
Upvote 0
Code:
Range("G4").Select
    ActiveSheet.PivotTables("PivotTable7").PivotFields("Site").AutoSort _
        xlDescending, "Sum of Water (Gallons)", ActiveSheet.PivotTables("PivotTable7"). _
        PivotColumnAxis.PivotLines(6), 1
 
Upvote 0

Forum statistics

Threads
1,215,309
Messages
6,124,180
Members
449,146
Latest member
el_gazar

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