• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Excel Pivot table slicer selection with VBA Code

Hi,

I have a workbook with soccer statistics in a data worksheet. I have made a pivot table from this data sheet. I am wondering if it is possible to select values between 1,00 and 1,50 in a pivot table slicer with VBA code.

I have attached the workbook with this information.

Kindly Regards
Lars Ole
 

Attachments

  • Excel Pivot table slicer selection with VBA.xlsm
    56 KB · Views: 19
Something like below. This assumes there's only one SlicerCache in the workbook and one PivotTable in the worksheet. If more than one, specify SlicerCache/PivotTable with name instead of index.

Code:
Sub Test()
Dim pvt As PivotTable
Dim sItem As SlicerItem
Set pvt = ThisWorkbook.Worksheets("Pivot Table").PivotTables(1)
pvt.ManualUpdate = True

With ThisWorkbook.SlicerCaches(1)
    .ClearAllFilters
    For Each sItem In .SlicerItems
        If sItem.Value >= 1 And sItem.Value <= 1.5 Then
            sItem.Selected = True
        Else
            sItem.Selected = False
        End If
    Next
End With

pvt.ManualUpdate = False
Set pvt = Nothing
End Sub
 
Back
Top