Excel

TheSmallman.com

an XL ideas Lab

Dashboards VBA
  • Home
  • Dashboards
    • Tips & Tricks
    • Charts
    • Modelling
    • Infographics
    • VBA
  • Shop Dashboards
    • Power Pivot a User Guide
    • Excel Dashboard Course
    • Advanced Dashboard Course
    • Financial Modelling Course
    • Excel VBA Course
  • Blog
  • About
Menu

Excel Dashboards VBA

Street Address
City, State, Zip
Phone Number
an XL ideas Lab

Your Custom Text Here

Excel Dashboards VBA

  • Home
  • Dashboards
  • Excel Tips
    • Tips & Tricks
    • Charts
    • Modelling
    • Infographics
    • VBA
  • Shop Dashboards
  • PowerPivot
    • Power Pivot a User Guide
  • Courses
    • Excel Dashboard Course
    • Advanced Dashboard Course
    • Financial Modelling Course
    • Excel VBA Course
  • Blog
  • About

Filter Source Data in Excel with Slicer

September 10, 2015 Marcus Small
nathan-dumlao-454456-unsplash.jpg

I was recently answering a post on Ozgrid about filtering a list using a slicer.  The post was very similar to a blog post of mine with a slight twist. The poster wanted the original list filtered based on the selection of the slicer and if no slicer item was selected then the filter was to be taken off the dataset.  I found the problem interesting because in order to solve the problem you had to know how many slicer items were in the list in the first place.  

How to solve a problem like this?  Well firstly you need to generate code which detects a change in the slicer.  The first thing to remember when dealing in slicers is you are not trapping a change in the slicer but a change in the source data, the pivot table.  So by trapping a change in the pivot table in turn you trap a change in the slicer.

In short we need a worksheet change event.  The following goes in the worksheet that the slicer is on.

Private Sub Worksheet_PivotTableChangeSync(ByVal Target As PivotTable)
   Dim slItem As SlicerItem
   Dim i As Integer

   Sheet1.AutoFilterMode = False

   With ActiveWorkbook.SlicerCaches("Slicer_Data")
      For Each slItem In .VisibleSlicerItems
         If slItem.Selected = True Then i = i + 1
      Next slItem
      If i <> .SlicerItems.Count Then Sheet1.[a1:A100].AutoFilter 1, Sheet3.[a4]
   End With
End Sub

Now the thing to remember about the data above is that the name of the slicer is called Slicer_Data.  You can obtain the name of your slicer by right clicking on the slicer and choosing:

Slicer Settings

From the dialog which is displayed you can see at the top of screen that the text has the name of the slicer.  Tricky to see if you don't know what you are looking for.

The list above has East as the selected slicer item.  The corresponding source data looks as follows when this happens.

In the slicer above if the red cross is pressed the filter is taken off and the full list is shown.  This is a very neat way to manage a list of data with an Excel slicer.

The following is the link to the Excel file which shows the workings of this procedure.  The file is the second article.

FIlter Source with Slicer

Tags Excel, VBA, Slicer change, count, slicer, items, spreadsheet
← Convert Vertical Range to Horizontal with VBA Excel VBA Colour Function →

Featured Posts

Excel Dashboards: Tracking a Crisis

Excel Dashboards: Tracking a Crisis
April 14, 2020

Recent Posts

Populating an Excel Table from a Range of Cells with VBA

Populating an Excel Table from a Range of Cells with VBA June 12, 2025

Fuzzy Distribution with Randbetween

Fuzzy Distribution with Randbetween May 21, 2025

Add Minimum and Maximum for Chart in Cells

Add Minimum and Maximum for Chart in Cells March 12, 2025

Inflation Over Multiple Years in a Single Cell

Inflation Over Multiple Years in a Single Cell January 10, 2025

Hubspot Dashboard

Hubspot Dashboard October 3, 2024

Monthly Dashboard With Supporting Metrics

Monthly Dashboard With Supporting Metrics September 25, 2024

Excel Show Missing Sheet Tabs

Excel Show Missing Sheet Tabs July 29, 2024

Run Macro Overnight Automatically

Run Macro Overnight Automatically June 24, 2024

Split File into Parts and Save to Directory

Split File into Parts and Save to Directory April 20, 2024

Most Popular Author

Most Popular Author December 14, 2023

 

Follow US:

 
 

MarcusSmall@thesmallman.com

 

TheSmallman.com - Making your small systems hum...
© Copyright 2013-2024 theSmallman.com All Rights Reserved.