【问题标题】:Inserting a date from a cell into a Pivot table to use it as a filter将单元格中的日期插入数据透视表以将其用作过滤器
【发布时间】:2017-01-16 20:25:24
【问题描述】:

我对 VBA 很陌生。我将解释我的项目。

我有一个 Excel 工作表,其中包含许多要过滤的信息。目前我的代码如下所示:

With ActiveSheet.PivotTables("Tableau croisé dynamique2").PivotFields( _
        "CREATION_DATE")
        .Orientation = xlPageField
        .Position = 1
    End With

ActiveSheet.PivotTables("Tableau croisé dynamique2").AddDataField ActiveSheet. _
    PivotTables("Tableau croisé dynamique2").PivotFields("AMOUNT"), _
    "Nombre de AMOUNT", xlCount

With ActiveSheet.PivotTables("Tableau croisé dynamique2").PivotFields("STATUS")
    .Orientation = xlRowField
    .Position = 1
End With

With ActiveSheet.PivotTables("Tableau croisé dynamique2").PivotFields( _
    "VENDOR_NAME")
    .Orientation = xlRowField
    .Position = 2
End With

With ActiveSheet.PivotTables("Tableau croisé dynamique2").PivotFields( _
    "INVOICE_NUM")
    .Orientation = xlRowField
    .Position = 3
End With

With ActiveSheet.PivotTables("Tableau croisé dynamique2").PivotFields( _
    "Nombre de AMOUNT")
    .Caption = "Somme de AMOUNT"
    .Function = xlSum
End With

ActiveSheet.PivotTables("Tableau croisé dynamique2").PivotFields( _
    "CREATION_DATE").CurrentPage = "08/09/2016"

此代码用于提取以过滤日期为 08/09/2016。 现在,我想让数据透视表在启动代码之前在单元格中获取此日期。

但我不想每次都进入代码来更改我想要的日期。

我想要这样的东西: 例如:我在单元格“A1”中写“09/09/2016”

ActiveSheet.PivotTables("Tableau croisé dynamique2").PivotFields( _
        "CREATION_DATE").CurrentPage = "A1"

但是我遇到了调试错误。

【问题讨论】:

  • .CurrentPage = Range("A1").Value2 ...您可能需要在其周围加上CDate()
  • 感谢您的回复,但它似乎不起作用。将 Cdate() 包裹起来是什么意思? ActiveSheet.PivotTables("Tableau croisé dynamique2").PivotFields(_"CREATION_DATE").CurrentPage = Range(CDate("'Sheet_date'!A1")).Value2
  • Scott 说如果... .CurrentPage = Range("A1").Value2 不起作用,请尝试使用... .CurrentPage = CDate(Range("A1").Value2)。 (CDate 将值转换为日期。)但它实际上可能需要一个字符串,因此,如果 Scott 的建议都不起作用,请尝试 ... .CurrentPage = Format(CDate(Range("A1").Value2),"dd/mm/yyyy")(或“mm/dd/yyyy” - 取决于该日期的格式)中)。
  • @Thistlepower 试试我下面答案中的代码,如果它有效,请告诉我
  • 谢谢!此代码有效! .CurrentPage = Format(CDate(Range("A1").Value2),"dd/mm/yyyy")

标签: vba excel date format


【解决方案1】:

经过一些测试,我找到了原因(在我的 Excel Book 上)。它与格式化数据透视字段“CREATION_DATE”有关,一旦我添加了以下部分,它就起作用了。

Set PvtFld = PvtTbl.PivotFields("CREATION_DATE")
With PvtFld
    .Orientation = xlPageField
    .NumberFormat = "dd/mm/yyyy"  ' << this line
    .Position = 1
End With

完整代码:

Sub PivotFilterDate()

Dim PvtTbl              As PivotTable
Dim PvtFld              As PivotField

' set your Pivot Table (after created) to a variable
Set PvtTbl = Sheets("PivotSpanish").PivotTables("Tableau croisé dynamique2")

' Set "CREATION_DATE" to a Pivot Field variable 
Set PvtFld = PvtTbl.PivotFields("CREATION_DATE")
With PvtFld
    .Orientation = xlPageField
    .NumberFormat = "dd/mm/yyyy"
    .Position = 1
End With

' you can modify your code to change all field setting, like following 2:
With PvtTbl.PivotFields("STATUS")
    .Orientation = xlRowField
    .Position = 1
End With

With PvtTbl.PivotFields("VENDOR_NAME")
    .Orientation = xlRowField
    .Position = 2
End With

' clear previous filters
PvtFld.ClearAllFilters

' --- trapping errors --- 
' added a Boolean Flag to trap scenarios where no Pivot Item found that matches the value in Cell A1
Dim PvtItemExists       As Boolean

PvtItemExists = False

' check all Pivot items in Pivot Field ("CREATION_DATE")
For Each PvtItem In PvtFld.PivotItems
    ' check if current Pivot Item equals the value in Cell A1
    If PvtItem.Value = CStr(CDate(Range("D1").Value)) Then
        PvtItemExists = True
        Exit For
    End If 

Next PvtItem

If PvtItemExists Then
    PvtFld.CurrentPage = CStr(CDate(Range("D1").Value))
Else
    MsgBox "No data matches for date " & CDate(Range("D1").Value) & " in Pivot Table", vbCritical
End If    

End Sub

【讨论】:

  • @Thistlepower 不客气,请标记为答案并点赞
猜你喜欢
  • 2019-01-09
  • 2021-10-20
  • 1970-01-01
  • 2021-12-19
  • 1970-01-01
  • 2021-11-15
  • 1970-01-01
  • 2017-03-29
  • 1970-01-01
相关资源
最近更新 更多