excel - 数据透视表过滤器未更改为循环中的每个值
问题描述
我看过很多类似的帖子,但没有一个能够解决我更改数据透视表过滤字段的问题。我试图通过一个循环进行过滤,该循环获取 ws2 列表中的每个值并将更改粘贴FilterID
到该值。但是,在我尝试过的所有不同方法中,将其设置为一个值,将其设置为一个字符串,使用"CurrentPage"
,它们都没有工作或导致 1004 错误。我最近的努力如下。如何"MatchFilter"
根据下面循环中的值来更改?
下面的方法只是给出一个 1004 Application or object not defined 错误。
我也尝试添加MatchFilter.Orientation = xlPageField
但没有成功。
Sub Filter_Test()
Dim ws1 As Worksheet
Set ws1 = Sheets("Order_Groupings")
'The match list is the list of unique Match IDs to filter through, pasting each in the filter
Dim ws2 As Worksheet
Set ws2 = Sheets("Match_List")
'Here we will paste the results from each optimizer run
Dim ws3 As Worksheet
Set ws3 = Sheets("Optimizer_Results")
Dim pt As PivotTable
Set pt = ws1.PivotTables("Order_Groupings")
Dim FilterID As String
Dim MatchFilter As PivotField
Set MatchFilter = pt.PivotFields("Match_ID")
Dim numIDs As Integer
'This is the number of different match IDs
'Match count is set to J4 right now in the Match List tab
numIDs = ws2.Range("match_count").Value
'For loop to cycle through each Match ID
For i = 1 To numIDs
FilterID = ws2.Range("A4").Offset(i, 0).Value
'Trying to set the MatchFilter to the new value in FilterID
With MatchFilter
.ClearAllFilters
.CurrentPageName = FilterID
End With
Next i
End Sub
解决方案
这最终通过事先清除所有过滤器并使用 PivotFilter.Add Type:=xlCpationEquals
Sub Filter_Test()
Dim ws1 As Worksheet
Set ws1 = Sheets("Order_Groupings")
'The match list is the list of unique Match IDs to filter through, pasting each in the filter
Dim ws2 As Worksheet
Set ws2 = Sheets("Match_List")
'Here we will paste the results from each optimizer run
Dim ws3 As Worksheet
Set ws3 = Sheets("Optimizer_Results")
Dim pt As PivotTable
Set pt = ws1.PivotTables("Order_Groupings")
Dim FilterID As String
Dim MatchFilter As PivotField
Set MatchFilter = pt.PivotFields("Match_IDs")
Dim numIDs As Integer
Dim i As Integer
'This is the number of different match IDs
'Match count is set to J4 right now in the Match List tab
numIDs = ws2.Range("match_count").Value
'For loop to cycle through each Match ID
For i = 1 To numIDs
FilterID = ws2.Range("A4").Offset(i, 0)
MatchFilter.ClearAllFilters
MatchFilter.PivotFilters.Add Type:=xlCaptionEquals, Value1:=FilterID
Next i
End Sub
推荐阅读
- ms-access - 如果有多个不同日期的记录,如何使用文本框在 Access 表单中获取记录
- google-chrome - Blue Prism 的 XE 转换器问题
- r - R中多列的一个样本Wilcoxon符号秩检验
- javascript - 不从上下文中消耗的组件仍然重新渲染
- python - 多标签分类和分层抽样,不同准备的目标值会得到不同的结果
- python - Django Rest Framework - 配置不当:无法解析超链接关系的 URL
- javascript - 无法在 IE 的窗口中存储对象
- arrays - “aMultiTable[i]”有什么作用,它有什么用?
- android - Spinner android根据时间(月)更改项目
- javascript - 如何停止 setInterval 在案例块中运行