I'm trying to filter a pivot table and it depends on a user criteria, which are always 12 values.

To create the criteria I have a range of possible values and the following code creates a new column of 1 or 0, one meaning include in the filter:

For i = 123 To 5 Step -1
    Cells(i, 60).Value = 0
    If Cells(i, 59).Value = Criterio Then Cells(i, 60).Value = 1
    If Cells(i, 59).Value = Criterio Then Indicador = Indicador + 1
    If Indicador > 0 And Indicador <= 12 Then Cells(i, 60).Value = 1
    If Indicador = 1 Then PivotSelection = Criterio & Chr(34)
    If Indicador > 1 And Indicador < 12 Then PivotSelection = PivotSelection & "," & Chr(34) & Cells(i, 59).Value & Chr(34)
    If Indicador = 12 Then PivotSelection = PivotSelection & "," & Chr(34) & Cells(i, 59).Value
    If Indicador > 0 Then Indicador = Indicador + 1
Next i

So it returns a string like: "2012.01","2012.02","2012.03",...,"2012.12" for example.

I thought the best way is to filter the pivot is using CASE. The problem is that when I use this criteria in the CASE option, it is not working:

ActiveSheet.PivotTables("Table").PivotFields("CLOSING_MONTH"). _
    CurrentPage = "(All)"
With ActiveSheet.PivotTables("Table").PivotFields("CLOSING_MONTH")
    For Each oPi In .PivotItems
        If oPi = "(blank)" Then Exit For
        Select Case oPi.Value
            Case PivotSelection
                If oPi.Visible = False Then oPi.Visible = True
            Case Else
                If oPi.Visible = True Then oPi.Visible = False
            End Select
    Next oPi
End With

Any idea?

有帮助吗?

解决方案

Try this instead of Select Case

If (InStr(1, PivotSelection, oPi.Value)) Then
    oPi.Visible = True
Else
    oPi.Visible = False
End If
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top