使用 VBA 选择数据透视表数据范围
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18098209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pivot table Data Range selection using VBA
提问by user1040563
Im trying to work on a data in a pivot table by selecting it first but except for the grand Totals. I still want the grand totals to be displayed. when I write:
我试图通过首先选择数据来处理数据透视表中的数据,但总计除外。我仍然希望显示总计。当我写:
ActiveSheet.PivotTables("PivotTable1").DataBodyRange.select
I get this:
我明白了:
(all the data including the grand totals is selected)
(包括总计在内的所有数据都被选中)
but I want it to be like this:
但我希望它是这样的:
How can I ignore the grand totals when selecting the data?
选择数据时如何忽略总计?
回答by Przemyslaw Remin
With Resize
you can change the size of selected range:
有了Resize
你可以改变的选择范围大小:
Dim rng As Range
Set rng = ActiveSheet.PivotTables(1).DataBodyRange
rng.Resize(rng.Rows.Count - 1, rng.Columns.Count - 1).Select
rng.Rows.Count - 1
is responsible for diminishing number of rows by 1. I had to squeeze the columns by 1 to exclude both grand totals out of the selection.
rng.Rows.Count - 1
负责将行数减少 1。我不得不将列压缩 1 以从选择中排除两个总计。
You can do it in one shot, if you do not like Dim
:
如果你不喜欢,你可以一次性完成Dim
:
ActiveSheet.PivotTables(1).DataBodyRange.Resize(ActiveSheet.PivotTables(1).DataBodyRange.Rows.Count - 1, ActiveSheet.PivotTables(1).DataBodyRange.Columns.Count-1).Select