VBA Pivot Table Fields

helpexcel

Well-known Member
Joined
Oct 21, 2009
Messages
656
I have code that creates a pivot table and it works fine. However I want to make it dynamic, by using the value in B1. ANy ideas on how to update this portion of the code?


Code:
With pvt    
.PivotFields("Color").Orientation = xlPageField
        With pvt.PivotFields("Color")
            On Error Resume Next
            .PivotItems = .Range("B1")
            On Error GoTo 0
        End With
End With
 
Last edited:

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Your .Range("B1") is applied to the With statement: With pvt.PivotFields("Color"), so the effective statement:
pvt.PivotFields("Color").Range("B1")
is causing an error.

Also you when filter a pivot field:
1) At least one PivotItem must always be visible
2) You have to explicitly make .Visible = False for the ones you do not want to see.

Code:
Option Explicit

Sub ShowOnePivotItem()
    Dim lPIIndex As Long
    Dim pvt As PivotTable
    Dim sItem As String
    
    sItem = Worksheets("Sheet1").Range("S3").Value
    
    Set pvt = ActiveSheet.PivotTables("PivotTable1")
    
    'Clear Filters
    pvt.PivotFields("Color").ClearAllFilters
    pvt.PivotFields("Color").EnableMultiplePageItems = True
    
    With pvt.PivotFields("Color")
        'Ensure the desired PivotItem exists by trying to make it visible
        On Error Resume Next
        .PivotItems(sItem).Visible = True
        If Err.Number <> 0 Then
            MsgBox "PivotItem " & sItem & " for PivotField Color does not exist."
            End
        End If
        'Hide the others
        For lPIIndex = 1 To pvt.PivotFields("Color").PivotItems.Count
            Select Case pvt.PivotFields("Color").PivotItems(lPIIndex)
            Case sItem
                .PivotItems(lPIIndex).Visible = True
            Case Else
                .PivotItems(lPIIndex).Visible = False
            End Select
        Next
        
    End With
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,627
Messages
6,120,610
Members
448,973
Latest member
ChristineC

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top