【问题标题】:Excel VBA to select PivotField and associated data values with that PivotField rowExcel VBA 选择 PivotField 和与该 PivotField 行关联的数据值
【发布时间】:2019-03-27 10:04:24
【问题描述】:

我想从行项目中选择与一个特定数据透视字段关联的所有数据单元格,我该怎么做?

我的数据如下所示:

          Sum of x  Sum of y  Sum of z
Class1       2.5        1         2
   *Name1    *1        *0        *0
   *Name2    *1        *1        *1
   *Name3    *.5       *0        *1
Class2       3.8       2.6        2
   *NameA    *1        *1        *0
   *NameB   *0.8       *0        *1
   *NameC    *1       *0.6       *0 
   *NameD    *1        *1        *1

现在,我只想选择前面带有 * 的数据并执行条件格式 - 如果单元格值小于 1,则突出显示该单元格。如果它大于 1,则用不同的颜色突出显示它。如上所述,我无法选择所需的数据范围。

这是尝试的代码:(错误:对象不支持此属性或方法)

Sub formatPivotTable()

Dim pt As PivotTable
Dim pf As PivotField
Dim pi As PivotItem

Set pt = ActiveSheet.PivotTables("test")

Set pf = pt.PivotFields("Name").PivotItems.DataRange.Select (error: object doesnt support this property or method)

With pf.DataRange
    .Interior.ColorIndex = 6
    .FormatConditions.Delete
    .FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, Formula1:="=1"
    With .FormatConditions(1)
        .Interior.ColorIndex = 3

    End With
    .FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, Formula1:="=1"
    With .FormatConditions(2)
        .Interior.ColorIndex = 4
    End With
End With

End Sub

任何帮助将不胜感激。

【问题讨论】:

  • 我认为您不需要Select 任何东西...未经测试,但根据documentation 中的示例,如果您将该行更改为Set pf = pt.PivotFields("Name") 会怎样?跨度>
  • @BigBen 只选择了我的行而不是数据字段。它选择了从 Class1 到 NameD 的所有行项目。我只想选择名称行和关联的数据字段。
  • 我一直发现 Jon Peltier 关于如何选择数据透视表范围的 explanation 很有帮助。如果我稍后有时间,我可能会再看看你的问题。
  • 感谢@BigBen,这会很有帮助。我确实看过 Jon Peltier 的解释,但我找不到我想要做什么。

标签: excel vba pivot-table


【解决方案1】:

希望这是您正在寻找的。​​p>

以下代码:

  1. 遍历“名称”pivotField 中的每个pivotItem,并使用Union 构建namesRangenamesRange 对应截图中的$B$5:$B$7,$B$9:$B$12
  2. 然后使用IntersectnamesRange.EntireRow和整个数据透视表的DataBodyRange得到condFormRangecondFormRange 对应截图中的$B$5:$D$7,$B$9:$D$12

从那里开始,条件格式就像你已经拥有的那样。

Sub FormatPivotTable()
    Dim pt As PivotTable
    Set pt = ActiveSheet.PivotTables("test")

    Dim pf As PivotField
    Set pf = pt.PivotFields("Name")

    Dim pi As PivotItem
    Dim namesRange As Range

    For Each pi In pf.PivotItems
        If namesRange Is Nothing Then
            Set namesRange = pi.DataRange
        Else
            Set namesRange = Union(namesRange, pi.DataRange)
        End If
    Next pi

    Debug.Print namesRange.Address ' returns $B$5:$B$7,$B$9:$B$12

    If Not namesRange Is Nothing Then
        Dim condFormRange As Range
        Set condFormRange = Intersect(namesRange.EntireRow, pt.DataBodyRange)
        Debug.Print condFormRange.Address ' returns $B$5:$D$7,$B$9:$D$12

        With condFormRange
            .Interior.ColorIndex = 6
            .FormatConditions.Delete
            .FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, Formula1:="=1"

            With .FormatConditions(1)
                .Interior.ColorIndex = 3
            End With

            .FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, Formula1:="=1"

            With .FormatConditions(2)
                .Interior.ColorIndex = 4
            End With
        End With
    End If
End Sub

【讨论】:

    猜你喜欢
    • 2015-08-15
    • 2013-03-12
    • 2017-03-25
    • 1970-01-01
    • 2017-03-01
    • 2019-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多