简体   繁体   English

Excel VBA自动筛选数据透视表

[英]Excel VBA Auto Filter Pivot Table

I have compiled VBA code to filter through each name in a pivot table and then call other subroutines to manipulate and format the table. 我已经编译了VBA代码,以筛选数据透视表中的每个名称,然后调用其他子例程来操纵和格式化该表。 This code has worked perfectly for me in the past but after updating the source table and making minor formatting changes to the pivot table it now fails me. 这段代码在过去对我来说是完美的,但是在更新源表并对数据透视表进行较小的格式更改后,它现在使我失败了。

Here is the code: 这是代码:

Sub AutoFilterEachName()

Piv_Sht = ActiveSheet.Name
Sheets(ActiveSheet.Name).Select
For Each PivotItem In ActiveSheet.PivotTables(1).PageFields(1).PivotItems
ActiveSheet.PivotTables(1).PageFields(1).CurrentPage = PivotItem.Value
Call PivotCopyFormatValues
Call DeleteRows2
Call DeleteRows2
Call AddComment
Call Move_Save_As

Sheets(Piv_Sht).Select

Next
End Sub

First: the line ActiveSheet.PivotTables(1).PageFields(1).CurrentPage = PivotItem.Value doesn't change the filter to the first value of the Name filter when the code is started. 首先: ActiveSheet.PivotTables(1).PageFields(1).CurrentPage = PivotItem.Value代码启动时不会将过滤器更改为Name过滤器的第一个值。

Second: When the routine reaches Next it goes to End Sub with out going back up to the For Each line. 第二:当例程到达Next它将转到End Sub ,而不会返回到For Each行。 I have tried adding Next PivotItem whit no results. 我尝试添加Next PivotItem没有结果。 So it runs through the code once and stops. 因此,它只运行一次代码,然后停止。

I am at a dead end so any help is appreciated. 我处于死胡同,因此我们将不胜感激。

Sorry for the 7 month delayed response; 对不起,延迟了7个月; if you still need help...I think this might help you achieve the result you are seeking: 如果您仍然需要帮助...我认为这可能会帮助您实现所需的结果:

Sub AutoFilterEachName()

    Dim pTbl As PivotTable
    Dim piv_sht As Worksheet
    Dim i As Long

    Set piv_sht = ThisWorkbook.ActiveSheet
    Set pTbl = piv_sht.PivotTables(1)

    piv_sht.Select

    For i = 1 To pTbl.PageFields(1).PivotItems.Count

       'set the PageField page equal to the name of the PivotItem at index i
       'if you need to cycle through multiple PageFields, consider a nested For loop or For Each loop
        pTbl.PageFields(1).CurrentPage = pTbl.PageFields(1).PivotItems(i).Name

        Call PivotCopyFormatValues
        Call DeleteRows2
        Call DeleteRows2
        Call AddComment
        Call Move_Save_As

        piv_sht.Select

    Next i

End Sub

One reason your For Next wasn't executing and going immediately to End Sub is because your PageFields were empty; 您的For Next无法执行并立即转到End Sub的原因PageFields是因为您的PageFields为空。 if there are no PageFields to loop through, then your loop will exit immediately. 如果没有PageFields可以循环通过,那么您的循环将立即退出。 One solution to this is to always make sure there is at least one PageField . 一种解决方案是始终确保至少有一个PageField You could achieve this by entering this code before your For Loop : 您可以通过在For Loop之前输入以下代码来实现此目的:

pTbl.PivotFields(1).Orientation = xlPageField

This will insert the first PivotField in your table as the PageField . 这会将表中的第一个PivotField插入为PageField

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM