使用 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 22:36:20  来源:igfitidea点击:

Pivot table Data Range selection using VBA

excelvbapivot-table

提问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: enter image description here

我明白了: 在此处输入图片说明

(all the data including the grand totals is selected)

(包括总计在内的所有数据都被选中)

but I want it to be like this: enter image description here

但我希望它是这样的: 在此处输入图片说明

How can I ignore the grand totals when selecting the data?

选择数据时如何忽略总计?

回答by Przemyslaw Remin

With Resizeyou 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 - 1is 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