Excel 基于变量范围筛选数据透视表

Excel 基于变量范围筛选数据透视表,excel,filter,pivot-table,vba,Excel,Filter,Pivot Table,Vba,我的目标是使用另一个工作表中的范围过滤透视表。这个范围从第三个工作表中提取数据,这是一个数据转储,每次使用它都会启动一系列公式并进行更改 我有下面的代码,但我可以看到它正在运行每个透视表字段,将其与范围进行比较,然后删除过滤器。我有32000个字段需要检查,因此当前宏太慢,无法使用 有人能帮我修复代码,使其只根据范围内非空的值进行过滤吗 Sub PT() Dim PT As PivotTable Dim PI As PivotItem Set PT = Sheets("Pivot_Sheet")

我的目标是使用另一个工作表中的范围过滤透视表。这个范围从第三个工作表中提取数据,这是一个数据转储,每次使用它都会启动一系列公式并进行更改

我有下面的代码,但我可以看到它正在运行每个透视表字段,将其与范围进行比较,然后删除过滤器。我有32000个字段需要检查,因此当前宏太慢,无法使用

有人能帮我修复代码,使其只根据范围内非空的值进行过滤吗

Sub PT()
Dim PT As PivotTable
Dim PI As PivotItem
Set PT = Sheets("Pivot_Sheet").PivotTables("PivotTable2")
With Sheets("Pivot_Sheet").PivotTables("PivotTable2").PivotFields("Product")
.ClearAllFilters
End With
For Each PI In PT.PivotFields("Product").PivotItems
PI.Visible = WorksheetFunction.CountIf(Sheets("Sheet1").Range("J2:J100"),
PI.Name) > 0
Next PI
Set PT = Nothing
End Sub

你的代码在很多方面都会很慢。如果您有兴趣了解筛选数据透视表时要避免的瓶颈,请阅读my

下面的代码应该可以帮助您开始。如果你有任何问题,就喊一声

Option Explicit

Sub FilterPivot()
Dim pt As PivotTable
Dim pf As PivotField
Dim pi As PivotItem
Dim i As Long
Dim vItem As Variant
Dim vList As Variant

Set pt = ActiveSheet.PivotTables("PivotTable2")
Set pf = pt.PivotFields("Product")

vList = Application.Transpose(ActiveWorkbook.Worksheets("Sheet1").Range("J2:J100"))

pt.ManualUpdate = True 'Stops PivotTable from refreshing after each PivotItem is changed

With pf

    'At least one item must remain visible in the PivotTable at all times, so make the first
    'item visible, and at the end of the routine, check if it actually  *should* be visible
    .PivotItems(1).Visible = True

    'Hide any other items that aren't already hidden.
    'Note that it is far quicker to check the status than to change it.
    ' So only hide each item if it isn't already hidden
    For i = 2 To .PivotItems.Count
        If .PivotItems(i).Visible Then .PivotItems(i).Visible = False
    Next i

    'Make the PivotItems of interest visible
    On Error Resume Next 'In case one of the items isn't found
    For Each vItem In vList
        .PivotItems(vItem).Visible = True
    Next vItem
    On Error GoTo 0

    'Hide the first PivotItem, unless it is one of the items of interest
    On Error Resume Next
    If InStr(UCase(Join(vList, "|")), UCase(.PivotItems(1))) = 0 Then .PivotItems(1).Visible = False
    If Err.Number <> 0 Then
        .ClearAllFilters
        MsgBox Title:="No Items Found", Prompt:="None of the desired items was found in the Pivot, so I have cleared the filter"
    End If
    On Error GoTo 0

End With

pt.ManualUpdate = False

End Sub
选项显式
子过滤器pivot()
数据透视表
Dim pf作为数据透视字段
Dim pi作为数据透视项
我想我会坚持多久
作为变体的暗维它
Dim vList作为变体
Set pt=ActiveSheet.PivotTables(“数据透视表2”)
设置pf=pt.数据透视字段(“产品”)
vList=Application.Transpose(Active工作簿.Worksheets(“Sheet1”).Range(“J2:J100”))
pt.ManualUpdate=True“在更改每个数据透视项后停止刷新数据透视表
与pf
'数据透视表中必须至少有一个项目始终可见,因此请选择第一个项目
'项目可见,在例程结束时,检查它是否确实*应该*可见
.PivotItems(1).可见=真
'隐藏尚未隐藏的任何其他项目。
请注意,检查状态要比更改状态快得多。
'因此,仅当每个项目尚未隐藏时才隐藏它
对于i=2到.PivotItems.Count
如果.PivotItems(i).Visible,则.PivotItems(i).Visible=False
接下来我
'使感兴趣的数据透视项可见
在出现错误时,如果找不到其中一项,请“继续下一步”
对于vList中的每个vItem
.PivotItems(vItem).Visible=True
下一个维特姆
错误转到0
'隐藏第一个数据透视项,除非它是感兴趣的项目之一
出错时继续下一步
如果InStr(UCase(Join(vList,“|”)、UCase(.PivotItems(1)))=0,则.PivotItems(1).Visible=False
如果错误号为0,则
.ClearAllFilters
MsgBox标题:=“未找到任何项目”,提示:=“在透视中未找到所需的项目,因此我已清除筛选器”
如果结束
错误转到0
以
pt.ManualUpdate=False
端接头

您的代码在很多方面都会很慢。如果您有兴趣了解筛选数据透视表时要避免的瓶颈,请阅读my

下面的代码应该可以帮助您开始。如果你有任何问题,就喊一声

Option Explicit

Sub FilterPivot()
Dim pt As PivotTable
Dim pf As PivotField
Dim pi As PivotItem
Dim i As Long
Dim vItem As Variant
Dim vList As Variant

Set pt = ActiveSheet.PivotTables("PivotTable2")
Set pf = pt.PivotFields("Product")

vList = Application.Transpose(ActiveWorkbook.Worksheets("Sheet1").Range("J2:J100"))

pt.ManualUpdate = True 'Stops PivotTable from refreshing after each PivotItem is changed

With pf

    'At least one item must remain visible in the PivotTable at all times, so make the first
    'item visible, and at the end of the routine, check if it actually  *should* be visible
    .PivotItems(1).Visible = True

    'Hide any other items that aren't already hidden.
    'Note that it is far quicker to check the status than to change it.
    ' So only hide each item if it isn't already hidden
    For i = 2 To .PivotItems.Count
        If .PivotItems(i).Visible Then .PivotItems(i).Visible = False
    Next i

    'Make the PivotItems of interest visible
    On Error Resume Next 'In case one of the items isn't found
    For Each vItem In vList
        .PivotItems(vItem).Visible = True
    Next vItem
    On Error GoTo 0

    'Hide the first PivotItem, unless it is one of the items of interest
    On Error Resume Next
    If InStr(UCase(Join(vList, "|")), UCase(.PivotItems(1))) = 0 Then .PivotItems(1).Visible = False
    If Err.Number <> 0 Then
        .ClearAllFilters
        MsgBox Title:="No Items Found", Prompt:="None of the desired items was found in the Pivot, so I have cleared the filter"
    End If
    On Error GoTo 0

End With

pt.ManualUpdate = False

End Sub
选项显式
子过滤器pivot()
数据透视表
Dim pf作为数据透视字段
Dim pi作为数据透视项
我想我会坚持多久
作为变体的暗维它
Dim vList作为变体
Set pt=ActiveSheet.PivotTables(“数据透视表2”)
设置pf=pt.数据透视字段(“产品”)
vList=Application.Transpose(Active工作簿.Worksheets(“Sheet1”).Range(“J2:J100”))
pt.ManualUpdate=True“在更改每个数据透视项后停止刷新数据透视表
与pf
'数据透视表中必须至少有一个项目始终可见,因此请选择第一个项目
'项目可见,在例程结束时,检查它是否确实*应该*可见
.PivotItems(1).可见=真
'隐藏尚未隐藏的任何其他项目。
请注意,检查状态要比更改状态快得多。
'因此,仅当每个项目尚未隐藏时才隐藏它
对于i=2到.PivotItems.Count
如果.PivotItems(i).Visible,则.PivotItems(i).Visible=False
接下来我
'使感兴趣的数据透视项可见
在出现错误时,如果找不到其中一项,请“继续下一步”
对于vList中的每个vItem
.PivotItems(vItem).Visible=True
下一个维特姆
错误转到0
'隐藏第一个数据透视项,除非它是感兴趣的项目之一
出错时继续下一步
如果InStr(UCase(Join(vList,“|”)、UCase(.PivotItems(1)))=0,则.PivotItems(1).Visible=False
如果错误号为0,则
.ClearAllFilters
MsgBox标题:=“未找到任何项目”,提示:=“在透视中未找到所需的项目,因此我已清除筛选器”
如果结束
错误转到0
以
pt.ManualUpdate=False
端接头

请将代码放入代码标签。对不起,代码已标记。请将代码放入代码标签。对不起,代码已标记。