显示数据透视表过滤器值

时间:2015-03-10 01:25:49

标签: vba excel-vba excel-2010 pivot-table excel

我有一个数据透视表,我已经应用了日期过滤器:

enter image description here

我正在寻找一种在单元格中显示过滤器信息的方法。

e.g。 2015年1月1日至2015年10月3日

我尝试了以下操作,只是让它在消息框中显示信息:

Sub DisplayRange()

    With ActiveSheet.PivotTables("OrdersPerSlot").PivotFields("PickDate").PivotFilters(1)
        MsgBox "FilterType: " & .FilterType & vbCr _
         & "Value1: " & .value1 & vbCr _
         & "Value2: " & .value2
    End With

End Sub

我收到以下错误:

enter image description here

接下来,我将代码移到了#34; ThisWorkBook"对象,以防有一些引用问题,并收到此错误:

enter image description here

1 个答案:

答案 0 :(得分:1)

我认为你需要VBA。通过在添加日期过滤器时运行宏记录器我想出了:

Sub GetPivotFilterDates()
Dim pvt As Excel.PivotTable
Dim pvtField As Excel.PivotField

Set pvt = Worksheets(1).PivotTables(1)
Set pvtField = pvt.PivotFields("Date Range")
With pvtField.PivotFilters(1)
    If .FilterType = xlDateBetween Then
        Worksheets(1).Range("A1").Value = "Filter is between " & .Value1 & " and " & .Value2
    End If
End With
End Sub