[英]Why isn't my code filtering my PivotTable?
I'm trying to filter multiple PivotTables at one time. 我正在尝试一次过滤多个数据透视表。 The error is occurring on the line beginning with
pt.PivotFields...
, not sure why it's not working: 该错误发生在以
pt.PivotFields...
开头的行上,不确定为什么它不起作用:
Sub PivotFilter()
Application.ScreenUpdating = False
Dim table As String
Dim tablenum As Integer
Dim comptable As String
table = "CD"
Dim Col1 As Integer
Dim Col2 As Integer
Dim Rng1 As Range
Dim Rng2 As Range
Dim Dt1 As Date
Dim Dt2 As Date
Dim pt As PivotTable
For tablenum = 2 To 61
comptable = table & tablenum
Col1 = tablenum * 2 + tablenum - 2
Col2 = tablenum * 2 + tablenum - 1
Set Rng1 = Sheets("Sheet2").Cells(1, Col1)
Set Rng2 = Sheets("Sheet2").Cells(1, Col2)
Dt1 = Rng1.Value
Dt2 = Rng2.Value
Set pt = ActiveSheet.PivotTables(comptable)
pt.PivotFields("Maturity Date").PivotFilters.Add Type:=xlDateBetween, Value1:=Rng1, Value2:=Rng2
Next tablenum
Application.ScreenUpdating = False
End Sub
I have two guesses as to what might be the problem: 对于可能是什么问题,我有两个猜测:
This should address either/both: 这应针对以下两者之一:
Dim d1, d2 as Date
d1 = CDate(Rng1.Value);
d2 = CDate(Rng2.Value);
Set pt = ActiveSheet.PivotTables(comptable)
pt.PivotFields("Maturity Date").ClearAllFilters
pt.PivotFields("Maturity Date").PivotFilters.Add _
Type:=xlDateBetween, Value1:=d1, Value2:=d2
Adding PivotFilters 添加数据透视过滤器
This error is due to the use of the wrong Data Type
to pass the parameters to the PivotFilters.Add2
Method . 此错误是由于使用了错误的
Data Type
将参数传递给PivotFilters.Add2
方法 PivotFilters.Add2
。 You are using Date
instead of Variant
(see PivotFilters.Add2 Method (Excel) ) 您正在使用
Date
而不是Variant
(请参阅PivotFilters.Add2方法(Excel) )
Below is your original code with some modifications to correct the Data Type
issue and other minor changes (commented in the code) . 以下是您的原始代码,其中进行了一些修改以更正
Data Type
问题和其他较小的更改(在代码中进行了注释) 。 It's also suggested to replace activesheet
with an object variable. 还建议用对象变量替换活动
activesheet
。
Sub PivotFilters_Add2()
Rem Suggest to use a constant instead of a variable
Const kTable As String = "CD"
Dim pt As PivotTable
Dim comptable As String
Rem Suggest to use Byte for numbers ranging in value from 0–255.
Dim tablenum As Byte, Col1 As Byte, Col2 As Byte
Dim vDte1 As Variant, vDte2 As Variant
Application.ScreenUpdating = False
For tablenum = 2 To 61
comptable = kTable & tablenum
Col1 = tablenum * 2 + tablenum - 2
Col2 = tablenum * 2 + tablenum - 1
'In this case Ranges are only used to retrieve the values for the filters therefore
'instead of using ranges just set the value directly into the corresponding variables
vDte1 = Sheets("Sheet2").Cells(1, Col1).Value2
vDte2 = Sheets("Sheet2").Cells(1, Col2).Value2
Set pt = ActiveSheet.PivotTables(comptable)
'Suggest to refresh the PivotCache
'Use this line if all PivotTables share a common PivotCache
If tablenum = 2 Then pt.PivotCache.Refresh
'Otherwise use this line instead: pt.PivotCache.Refresh
With pt.PivotFields("Maturity Date")
.ClearAllFilters
.PivotFilters.Add2 Type:=xlDateBetween, Value1:=vDte1, Value2:=vDte2
End With: Next
Application.ScreenUpdating = False
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.