添加到 Siddharth Rout 的答案中,这是我发现的地方和内容:
[ http://www.globaliconnect.com/excel/index.php?option=com_content&view=article&id=153:excel-pivot-tables-grouping-group-items-group-data-and-group-date-values-using -vba&catid=79&Itemid=475][1]
使用分组方法对数字项或日期值进行分组。语法:RangeObjectVariable.Group(Start, End, By, Periods)。Start、End、By 和 Periods 的所有 4 个参数都是可选的,并在下面解释。
开始- 分组从此值开始,如果省略或 True 表示字段中的第一个值。
End - 分组在此值处结束,如果省略或 True 表示字段中的最后一个值。
By - 指定数字字段的组大小;对于日期字段,它指示天数,以防数组元素 4(天)设置为 True,并且 Periods 参数中的所有其他元素都为 False,否则此参数将被忽略并为组选择默认大小。
句点 - 7 个布尔值的数组:
- 元素 1 表示秒周期
- 元素 2 是分钟
- 元素 3 是小时
- 元素 4 是天
- 元素 5 是月份
- 元素 6 是宿舍
- 元素 7 是年。
Bollean 值为 True 的元素表示创建组的时间段。Periods 的这个参数仅对 Date 字段有效。
可能需要注意的是 RangeObject 只能是单个单元格,否则该方法将失败而不会显示任何错误。
Sub PivotTableGroupData1()
Dim PvtTbl As PivotTable
Dim rngGroup As Range
Set PvtTbl = Worksheets("Sheet6").PivotTables("PivotTable1")
'set range of dates to be grouped
Set rngGroup = PvtTbl.PivotFields("Dates").DataRange
'rngGroup.Cells(1) indicates the first cell in the range of rngGroup - remember that the RangeObject in the Group Method should only be a single cell otherwise the method will fail.
rngGroup.Cells(1).Group Periods:=Array(False, False, False, True, True, True, False)
'to ungroup:
'rngGroup.Cells(1).Ungroup
End Sub
我想补充一点,我花了几个月的时间才找到句号的确切定义。以为我会分享...