Kunj Patel
Kunj Patel

Reputation: 75

Excel VBA to select PivotField and associated data values with that PivotField row

I want to select all the data cells associated with one particular PivotField from the Row items, how do i do that?

My data looks something like this:

          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

Now, I only wanna select the data with * in front and perform conditional formatting - if the cell value is less than 1, highlight the cell. If it's greater than 1, highlight it with different color. I am having trouble selecting the data range I want as explained above.

Here is attempted code: (error: object doesn't support this property or method)

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

Any help would be appreciated.

Upvotes: 1

Views: 1837

Answers (1)

BigBen
BigBen

Reputation: 50008

Hopefully this is what you're looking for.

The code below:

  1. Loops through each pivotItem in the "Name" pivotField and builds up a namesRange using Union. The namesRange corresponds to $B$5:$B$7,$B$9:$B$12 in the screenshot.
  2. Then uses Intersect, namesRange.EntireRow and the DataBodyRange of the entire pivot table to get a condFormRange. The condFormRange corresponds to $B$5:$D$7,$B$9:$D$12 in the screenshot.

From there the conditional formatting is as you already have it.

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

enter image description here

Upvotes: 1

Related Questions