【发布时间】:2020-02-25 20:36:57
【问题描述】:
我是这个论坛的新手,非常感谢关于我过去 4 天一直在处理的问题的一些指导。
我有一个宏来过滤数据透视表,以便在同一张表中的单元格 B5 上的某个日期之前的日期。不幸的是,它产生了一些数据,但不是所有数据。
以下是我拥有的代码(基于研究/复制粘贴),如果代码不是最佳实践,我深表歉意。 我还附上了一张图片供参考。提前感谢您的评论和支持。
'Sub BeforeDate()
Dim sSheetName As String
Dim sPivotName As String
Dim sFieldName As String
Dim sFilterCrit As String
Dim pi As PivotItem
'Set the variables
sSheetName = "PivotTable"
sPivotName = "PivotTable1"
sFieldName = "apptdate"
'sFilterCrit = "5/2/2019"
sFilterCrit = ThisWorkbook.Worksheets(sSheetName).Range("B5").Value
With ThisWorkbook.Worksheets(sSheetName).PivotTables(sPivotName).PivotFields(sFieldName)
'Clear all filter of the pivotfield
.ClearAllFilters
'Loop through pivot items of the pivot field
'Hide or filter out items that do not match the criteria
For Each pi In .PivotItems
If pi > Range("B5") Then
pi.Visible = False
Else
pi.Visible = True
End If
Next pi
End With
End Sub
【问题讨论】: