excel - VBA,根据输入值更改数据透视字段“过滤器”
问题描述
以前我问过关于如何更改数据透视域“行”的问题(VBA,应用程序定义或对象错误,更改数据透视过滤器)。但是,更改“过滤器”字段似乎有所不同。
这是我用来用 vba 更改可透视表的“行”值的代码,Period
它是我的透视表中的“过滤器”字段。当我运行它时,我收到一条错误消息Could not apply filter
,这是用户错误处理。
Dim pivTable1 As PivotTable
Set pivTable1 = GetPivotTable(ThisWorkbook, "Summary of LoBs", "PivotTable1")
If pivTable1 Is Nothing Then
MsgBox "Missing Pivot Table", vbInformation, "Cancelled"
Exit Sub
End If
pivTable1.PivotCache.Refresh
Dim periodField As PivotField
On Error Resume Next
Set periodField = pivTable1.PivotFields("Period")
On Error GoTo 0
If periodField Is Nothing Then
MsgBox "Missing Pivot Field", vbInformation, "Cancelled"
Exit Sub
End If
periodField.ClearAllFilters
Dim filterDate As Variant
On Error Resume Next
filterDate = ThisWorkbook.Worksheets("Input").Range("H2").Value2
If Err.Number <> 0 Then
Err.Clear
MsgBox "Missing Filter Date", vbInformation, "Cancelled"
Exit Sub
End If
'Try String first
If VarType(filterDate) = vbString Then
periodField.PivotFilters.Add Type:=xlCaptionEquals, Value1:=filterDate
If Err.Number = 0 Then Exit Sub
filterDate = CDbl(CDate(filterDate))
Err.Clear
End If
If VarType(filterDate) <> vbDouble Then
MsgBox "Invalid Filter Date", vbInformation, "Cancelled"
Exit Sub
End If
'Try Date (as Double data type)
periodField.PivotFilters.Add Type:=xlSpecificDate, Value1:=filterDate
If Err.Number <> 0 Then
Err.Clear
MsgBox "Could not apply filter", vbInformation, "Cancelled"
Exit Sub
End If
任何帮助,将不胜感激。谢谢。
解决方案
我添加了一个解决这两种情况(行或字段)的选择案例:
Sub ApplyFilter()
Dim pivTable1 As PivotTable
Set pivTable1 = GetPivotTable(ThisWorkbook, "Summary of LoBs", "PivotTable1")
If pivTable1 Is Nothing Then
MsgBox "Missing Pivot Table", vbInformation, "Cancelled"
Exit Sub
End If
pivTable1.PivotCache.Refresh
Dim periodField As PivotField
On Error Resume Next
Set periodField = pivTable1.PivotFields("Period")
On Error GoTo 0
If periodField Is Nothing Then
MsgBox "Missing Pivot Field", vbInformation, "Cancelled"
Exit Sub
End If
periodField.ClearAllFilters
Dim filterDate As Variant
On Error Resume Next
filterDate = ThisWorkbook.Worksheets("Input").Range("H2").Value2
If Err.Number <> 0 Then
Err.Clear
MsgBox "Missing Filter Date", vbInformation, "Cancelled"
Exit Sub
End If
Select Case periodField.Orientation
Case xlRowField
'Try String first
If VarType(filterDate) = vbString Then
periodField.PivotFilters.Add Type:=xlCaptionEquals, Value1:=filterDate
If Err.Number = 0 Then Exit Sub
filterDate = CDbl(CDate(filterDate))
Err.Clear
End If
If VarType(filterDate) <> vbDouble Then
MsgBox "Invalid Filter Date", vbInformation, "Cancelled"
Exit Sub
End If
'Try Date (as Double data type)
periodField.PivotFilters.Add Type:=xlSpecificDate, Value1:=filterDate
If Err.Number <> 0 Then
Err.Clear
MsgBox "Could not apply filter", vbInformation, "Cancelled"
Exit Sub
End If
Case xlPageField
Dim pivItem As PivotItem
Dim v As Variant
For Each pivItem In periodField.PivotItems
v = pivItem.Value
If VarType(v) = VarType(filterDate) Then
pivItem.Visible = (v = filterDate)
Else
'Try converting to date
If IsDate(v) And IsDate(filterDate) Then
pivItem.Visible = (CDate(v) = CDate(filterDate))
Else
'Add other logic based on your needs
Err.Raise 5, , "Need more code here"
End If
End If
Next pivItem
Case Else
MsgBox "Could not apply filter!" & vbNewLine & "Field must be a <Row> or <Filter> field!", vbInformation, "Cancelled"
End Select
End Sub
但是,您会看到一条评论'Add other logic based on your needs"
。如果此处的代码不能解决问题,则需要根据数据类型添加逻辑。
推荐阅读
- python - 使用 InceptionV3 的迁移学习时,1 epoch 后未显示验证准确度
- python - wait_for 检查不起作用 discord.py
- python-3.x - 如何从 Instagram 抓取用户详细信息?
- node.js - 如何通过查找功能检查猫鼬中的存在和空?
- ios - 注入 III:与 BitCode 的可插入冲突
- python-3.x - expr*、expr 有什么区别?Python3.6 AST 抽象语法中的 expr 和 expr?
- sql - Oracle PL/SQL - 如何在 select 语句中使用 varchar 表?
- c - const 指针的声明与 const 整数的声明
- django - Getting the following error: Could not find a version that satisfies the requirement command-not-found==0.3
- python - Python中的交叉表和卡方检验