Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deselect all items in a pivot table using vba

Tags:

excel

vba

Can some quicly explain the way to deselect all items in a newly created pivot table so that I can go back and select only one or two items? I tried the following:

.PivotItems("(Select All)").Visible = False

Thanks.

like image 932
Chris2015 Avatar asked Dec 07 '12 17:12

Chris2015


People also ask

How do I remove all fields from a PivotTable?

To remove all report filters, labels, values, and formatting from a PivotTable, and to start designing the layout all over again, use the Clear All command. Click the PivotTable or PivotChart. On the Analyze tab, in the Actions group, click Clear, and then click Clear All.

How do you deselect columns in Excel VBA?

Deselect Specific Columns in Excel Below are the steps to so this: Select the entire sheet by clicking on the triangle at the top-left part of the sheet (or use the keyboard shortcut Control + A) Hold the Control key and select the columns that you want to remove from the selection (#5 and #9 in our example)

How do you select all values in a PivotTable?

Select an entire report Click the PivotTable. On the Options tab, in the Actions group, click Select, and then click Entire PivotTable.


2 Answers

This is probably the closest you can get to what you want:

Dim i As Long
.PivotItems(1).Visible = True
For i = 2 To .PivotItems.Count
    .PivotItems(i).Visible = False
Next

This will make the very first option the only selected option (assuming this is within a with that points to the pivotfield). If you know what you want before hand... modify accordingly.

like image 174
Daniel Avatar answered Oct 21 '22 14:10

Daniel


I've found that looping through each data item takes a lot of time, what you can do if you want to filter on a single item in a pivot without looping through all items is use the following code:

ActiveSheet.PivotTables("Your Pivot Name").PivotFields("Your Field Name").ClearAllFilters
ActiveSheet.PivotTables("Your Pivot Name").PivotFields("Your Field Name").PivotFilters.Add _
    Type:=xlCaptionEquals, Value1:="Your string here" 

this is basically a label filter but it worked for me.

like image 2
gilad Avatar answered Oct 21 '22 15:10

gilad