在数据透视表中过滤 - Excel

时间:2016-01-18 18:07:53

标签: excel vba pivot-table

我无法在Excel中过滤数据透视表。我试图使用True和False方法,但没有成功,因为它生成以下错误“无法获取PivotField PivotItems类的属性”(err.number = 1004)。在我尝试使用For Each之后。对于每个我设法更改过滤器,因为我不会更改我的数据透视表上的第一项。

方法True - False

Sub TandF()
    ActiveSheet.PivotTables("Tabela dinâmica2").ClearAllFilters
    With ActiveSheet.PivotTables("Tabela dinâmica2").PivotFields("Mês")
        .PivotItems("01/01/2015").Visible = False
        .PivotItems("02/01/2015").Visible = True
    End With
End Sub

方法每个

Function filter(dtIni As String, dtFim As String) As Boolean
    On Error GoTo e
    Dim PvtTbl As PivotTable
    Dim pvtItm As PivotItem
    dtIni = Format(dtIni, "mm/dd/yyyy")
    dtFim = Format(dtFim, "mm/dd/yyyy")

    Set PvtTbl = ActiveSheet.PivotTables("Tabela dinâmica2")
    ActiveSheet.PivotTables("Tabela dinâmica2").ClearAllFilters

    For Each pvtItm In PvtTbl.PivotFields("Mês").PivotItems
        If (pvtItm <> dtIni) And (pvtItm <> dtFim) Then
            pvtItm.Visible = False
        End If
    Next
    filter = True
    Exit Function
e:
    MsgBox "Erro: " & Err.Number & vbCrLf & Err.Description, vbCritical, "Erro ao Adicionar Filtros"
    filter = False

End Function

0 个答案:

没有答案