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

Change Pivot Table Filter From Cell

October 17, 2020 Marcus Small
Refresh Disconnected Pivot Table

Recently I had an issue, there were two tables which were disconnected and I needed the first pivot table to be refreshed off the output of the second pivot table when these were using different data sources. I solved this problem with a little VBA and a worksheet change event. The following was the first list - notice the TOTAL at the bottom. This is important as I want to consolidate on my dashboard page with a slicer but I am using calculation tables and a single pivot tables to summarise the datasets. The file attached is a sample of a much larger dataset.


I have created a YouTube video outlining the procedure. The start file is as follows:

Disconnected Pivot Tables.xlsm

The following is the small list that the pivot table and slicer will be built on.

Pivot Tables with VBA

The above is the starting list with the ALL at the base. Creating a slicer off the raw data (not the above) looks as follows.

List2.PNG

If all items are selected then that is the TOTAL - however if I want ta consolidation button for ALL I have to change it up a bit.

Now the database won’t have a field called ALL and this is important as I don’t want the user to click all the buttons (or unfilter everything) in order to show the TOTAL for all positions. Here is an example, it shows a pivot table summarised by ALL departments.

Pivot table Slicer with Total

Notice the addition of ALL at the end of the slicer. This will be our consolidation and we will manipulate the pivot table when this disconnected slicer is clicked. The raw pivot table that will feed our summary is a top five by the labour column and it will look as follows:

Pivot Table List

A summary by TOTAL is represented by the term ALL, so I want the pivot table to show the sum of the parts and I want the ability to summarise by ALL which is the TOTAL. The following is what ALL looks like:

List3.PNG

There is a new disconnected list on the LIST Sheet and this will be used for the slicer to control the output sheet. This worksheet contains the pivot table the ultimate slicer is built upon.

Using VBA to Update the Pivot Table

As the two lists are from different datasets a little VBA code is needed. The code to change the pivot table is as follows.

Option Explicit

Sub ChangePiv()
  Dim PT As PivotTable
  Dim PF As PivotField
  Dim str As String

  Set PT = Sheet4.PivotTables("PivotTable1")
  Set PF = PT.PivotFields("List")
  str = Sheet2.[B1].Value

  PF.ClearAllFilters
  PF.CurrentPage = str

End Sub

There are a few things you will need to change above to match your data set. The first is the name of the worksheet code name

  • Sheet4

The next is the name of the name pivot table:

  • PivotTable1

The next is the name of your pivotfield:

  • “List”

    An the final part is where the cell that you wish to control the pivot table is, be aware of the sheet it is on and the cell:

  • str = Sheet2.[B1].Value

You may want to do this from a data validation list or you could do it as I am presenting through a secondary slicer. Once these three things are changed you should be ready for the final part. This is creating the on cell change code that will run the macro every time your data validation list or slicer is clicked on.

The final bit of code need to go in the sheet that the slicer comes from - in my case it is the LIST tab worksheet code.

The ON Change Code

Option Explicit

Private Sub Worksheet_PivotTableChangeSync(ByVal Target As PivotTable)
    ChangePiv
End Sub

Inside the above code is the name of the macro that I want to run every time the pivot table changes and this will be through the slicer click.

The Final Product

The following is what the model looks like once complete. I have enhanced it a little

Pivot Table change from cell

It is a more intuitive method in my opinion of showing a total than by clicking all the buttons but others may feel differently. It is naturally a lot more work to create this method. Each to their own. The following Excel file should help you see the workings behind the above. You will need to understand the concept to apply it to your problem though. I will add a video in time to this article.

Excel Pivot Table Update.xlsm

← Excel Dashboard ExampleExcel Dashboard Designs →

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.