Search code examples
vbapivot-chart

How to make all items except "(blank)" visible in an excel pivot chart?


When recording the deselection of the "(Blank)" items I get an explicit mention of all items.

ActiveChart.PivotLayout.PivotTable _
.PivotFields("[Table1].[Field1].[Field1]").VisibleItemsList = _
                                           Array("[Table1].[Field1].&[Item1]", _
                                                 "[Table1].[Field1].&[Item2]", _
                                                 "[Table1].[Field1].&[Item3]", _
                                                 "[Table1].[Field1].&[Item4]", _
                                                 "[Table1].[Field1].&[Item5]", _
                                                 "[Table1].[Field1].&[Item6]")

But I can't mention the items since I don't know them beforehand (and they are located in the data model).

The code should be along the lines of:

With ActiveChart.PivotLayout.PivotTable 
    .PivotFields("[Table1].[Field1].[Field1]").VisibleItemsList = Array("")
    .PivotFields("[Table1].[Field1].[Field1]").PivotItems("(blank)").Visible = False
End With 

EDIT: Apparently, .PivotItems("(blank)").Visible = False should be doing exactly what I need, but it is not... (Runtime '1004': Unable to get the PivotItems property of the PivotField class)


Solution

  • It's been a while, but I had the same issue and just found a different solution that worked in my situation.

    Rather than record 'deselecting blank' which I did at first but didn't work, I recorded a 'Label Filter' with "does not equal:" (with nothing entered in the text box). That then gave me the following code (slightly modified since I'm using a with statement):

    .PivotFields("[Range].[Street Direction Code].[Street Direction Code]").PivotFilters.Add2 _
            Type:=xlCaptionDoesNotEqual, Value1:=""