简体   繁体   English

Excel VBA-在可变大小的数据透视表后隐藏行范围

[英]Excel VBA - Hide range of rows after variable sized pivot table

I trying to write a macro to format a number of sheets containing pivot tables. 我试图编写一个宏来格式化许多包含数据透视表的工作表。 I am stuck on this one problem. 我被困在这个问题上。

My sheet contains a number of stacked pivot tables. 我的工作表包含许多堆叠的数据透视表。 I need the VBA code snippet to hide, say, 1000 rows after the first table (or all tables except the top one if possible). 我需要VBA代码段来隐藏第一个表(或者如果可能的话,除最上面的表之外的所有表)后的1000行。 The top table will vary in size from one day to the next so it is not possible to just hide a set range. 顶层表格的大小在一天到第二天之间会有所不同,因此无法仅隐藏设定的范围。

Thanks 谢谢

To hide all the pivot tables except one, you may try something like this... In the below code, change the name of the pivot table you don't want to hide. 要隐藏除一个之外的所有数据透视表,您可以尝试类似的操作...在下面的代码中,更改您不想隐藏的数据透视表的名称。

Sub HideAllPivotTablesButOne()
Dim ws As Worksheet
Dim pt As PivotTable
Application.ScreenUpdating = False
Set ws = ActiveSheet
lr = ws.UsedRange.Rows.Count
For Each pt In ws.PivotTables
    If pt.Name <> "PivotTable1" Then     'Name of the Pivot Table which you don't want to hide
        pt.TableRange2.EntireRow.Hidden = True
    End If
Next pt
Application.ScreenUpdating = True
End Sub

If you want to show all the pivot tables again, you may try the below code... 如果要再次显示所有数据透视表,可以尝试以下代码...

Sub ShowAllPivotTables()
Dim ws As Worksheet
Dim pt As PivotTable
Application.ScreenUpdating = False
Set ws = ActiveSheet
lr = ws.UsedRange.Rows.Count
For Each pt In ws.PivotTables
    pt.TableRange2.EntireRow.Hidden = False
Next pt
Application.ScreenUpdating = True

End Sub

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

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