【问题标题】:How to reference cells in a Pivot Table using Field values?如何使用字段值引用数据透视表中的单元格?
【发布时间】:2015-02-24 03:23:38
【问题描述】:

我有一个从 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

【问题讨论】:

  • 已修复:将错误的第一个参数发送到 GetPivotDate。它不是数据字段。通过发送(在我的情况下)“SUM”作为第一个参数来解决。谢谢! :)

标签: excel vba pivot-table


【解决方案1】:

不确定,但也许您想要GetPivotData 函数?

【讨论】:

猜你喜欢
  • 2018-03-30
  • 1970-01-01
  • 2017-08-28
  • 1970-01-01
  • 2018-02-18
  • 2017-07-04
  • 1970-01-01
  • 2016-09-25
  • 1970-01-01
相关资源
最近更新 更多