Manipulating Pivot Fields with VBS

Dimaa

New Member
Joined
Apr 2, 2011
Messages
8
Hi all,

I'm new to Excel VBA and first of all, thank you to all of you who so generously give your time to help newbies with problems.

I'm writing code for a shortcut that will toggle between removing or reinserting the '0' pivot item from the row and column fields of an active pivot table. I've encountered 3 problems:

1 - I know best programming practice is not to select objects before manipulating them. But the code I've written seems to only work if the user manually selects the right object [row or column field] within the pivot table. That's not very good, so I've selected the row and column fields in vba. I'd love to know how to get around this.

2 - I've not figured out how to select both row and column fields of a pivot table together. I've tried the union method for the pivot row and column ranges, but it doesn't work. Listing mutliple ranges as arguments to a Range [which is what the Excel macro recorder does when I record manual selections] doesn't work either.

3 - The work around I figured out is to separate removing the '0' from the row and column fields into 2 independent procedures. I tried including these instructions, one after the other, in one sub procedure, but somehow it doesn't work. It does work when I separate them into 2 sub procedures. I thought I'd then use a third sub procedure to call and run both of the needed sub procedures. But that doesn't work and I can't figure out where I've gone wrong.

Here's the code I'm using:

This is one of the sub's for removing '0's from pivot row fields. The one for columns follows this model.

Code:
Sub RemoveZerosFromPivotRow()

Dim PT As PivotTable
Dim PF As PivotField

On Error GoTo NoPivot:

Set PT = ActiveCell.PivotTable
Set PF = ActiveCell.PivotField

PT.RowRange.Select

If PT.RowFields(PF.Name).PivotItems("0").Visible = True Then

PT.RowFields(PF.Name).PivotItems("0").Visible = False
    
Else

PT.RowFields(PF.Name).PivotItems("0").Visible = True
    
End If

NoPivot:
    Exit Sub
    Resume Next

End Sub

The code I used for running the 2 sub procedures looks like this. I've stored all three sub's in the code for worksheet1 for testing purposes.


Code:
Sub Test()
Call RemoveZerosFromPivotColumn
Call RemoveZerosFromPivotRow

Application.Run "RemoveZerosFromPivotColumn"
Application.Run "RemoveZerosFromPivotRow"

End Sub


Any help or suggestions much appreciated!
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

Forum statistics

Threads
1,224,574
Messages
6,179,633
Members
452,933
Latest member
patv

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