如何使用字段值引用数据透视表中的单元格?
我有一个由 3 个字段创建的数据透视表,其中(例如):
field1 值为:"1", "3", "5"
field2 值为:“A”、“B”、“C”
field3 值为:“X”、“Y”、“Z”
因此,给定一个数据透视表 PT1,我想以与 PT1["1" 类似的方式引用表中的单元格]["B"]["Y"]
并获取返回的 Range 项。这可能吗?谢谢。
编辑:代码:
Public Sub ColorIt2()
Dim rng As Range
For Each t_item In ActiveSheet.PivotTables("PivotTable1").PivotFields("F1").PivotItems
For Each r_item In ActiveSheet.PivotTables("PivotTable1").PivotFields("F2").PivotItems
For Each h_item In ActiveSheet.PivotTables("PivotTable1").PivotFields("F3").PivotItems
For Each b_item In ActiveSheet.PivotTables("PivotTable1").PivotFields("F4").PivotItems
If t_item.RecordCount <> 0 Or _
r_item.RecordCount <> 0 Or _
h_item.RecordCount <> 0 Or _
b_item.RecordCount <> 0 Then
Set rng = ActiveSheet.PivotTables("PivotTable1").GetPivotData(t_item, r_item, h_item, b_item)
rng.Select
Selection.Interior.ColorIndex = 40
Selection.Interior.Pattern = xlSolid
End If
End If
Next b_item
Next h_item
Next r_item
Next t_item
End Sub
I have a pivot table created from 3 fields where (for example):
field1 values are: "1", "3", "5"
field2 values are: "A", "B", "C"
field3 values are: "X", "Y", "Z"
So given a pivot table PT1 I want to reference a cell within the table in a similar way to PT1["1"]["B"]["Y"]
and get a Range item returned. Is this possible? Thanks.
EDIT: codes:
Public Sub ColorIt2()
Dim rng As Range
For Each t_item In ActiveSheet.PivotTables("PivotTable1").PivotFields("F1").PivotItems
For Each r_item In ActiveSheet.PivotTables("PivotTable1").PivotFields("F2").PivotItems
For Each h_item In ActiveSheet.PivotTables("PivotTable1").PivotFields("F3").PivotItems
For Each b_item In ActiveSheet.PivotTables("PivotTable1").PivotFields("F4").PivotItems
If t_item.RecordCount <> 0 Or _
r_item.RecordCount <> 0 Or _
h_item.RecordCount <> 0 Or _
b_item.RecordCount <> 0 Then
Set rng = ActiveSheet.PivotTables("PivotTable1").GetPivotData(t_item, r_item, h_item, b_item)
rng.Select
Selection.Interior.ColorIndex = 40
Selection.Interior.Pattern = xlSolid
End If
End If
Next b_item
Next h_item
Next r_item
Next t_item
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定,但也许您需要 GetPivotData 函数?
Not sure, but maybe you want the GetPivotData function?