MrExcel Publishing
Your One Stop for Excel Tips & Solutions

Sync Slicers from Different Data Sets


June 22, 2017 - by Bill Jelen

Sync Slicers from Different Data Sets

Slicers are awesome for pivot tables because you can control multiple pivot tables from one set of slicers. But - that is sort of a lie. You can control multiple pivot tables that came from the same data set. When you have pivot tables that came from two different data sets, it is pretty tricky. I will show you some VBA that will let you pull this off.

Watch Video

  • How can you have a slicer drive two pivot tables?
  • If both pivot tables came from the same data set: Select Slicer, Report Connections, Choose Other Pivot Tables
  • But if the pivot tables came from different data sets:
  • Use Save As to change the workbook extension to XLSM instead of XLSX
  • Use Alt + T M S and change macro security to second setting.
  • Alt + F11 to get to VBA
  • Ctrl + R to display the project explorer
  • Find the worksheet that contains your first pivot table and slicer
  • Insert the code for Worksheet_Update
  • Hide the second slicer away so it keeps existing but no one can ever choose from that slicer

Video Transcript

Learn Excel for MrExcel Podcast, Episode 2104: Synchronize Slicers from Different Data Sets.

Hey, welcome back to the MrExcel netcast, I'm Bill Jelen, and today's question is not about how to take these two pivot tables that came from one data set and make the Slicer control all those pivot tables. That's not what this is about. That's an easy thing to do-- Slicer, Tools, Options, either Report Connections or Slicer Connections in the old version, and check that you want this Slicer to control all of those pivot tables. Easy, right? This question is about this worksheet, where we have two different data sets and we're going to create a pivot table from this, and from this-- now let me speed up the video while I create these pivot tables. Alright, now, what you're going to see is, I have two pivot tables, this pivot table is created from one data set, and there's a slicer that controls that pivot table; and then I have a second pivot table that's created from a different data set, and a slicer that controls that pivot table. But there's absolutely no way to get this slicer to control both this pivot table and this pivot table that's built from a different data set. Alright. But I'm going to show you how to do that today with a macro.

Now, this is tricky to do. When the question came in, I said, "Now, this, I don't think you can do it." But I've been working on it and experimenting and I think I finally got it. I have to think I finally got it down. Alright, so let's go through this. First off, this is saved as an xlsx file. That's a fine file-type, except for it's a horrible file-type because it's the only file type that doesn't allow macros. You have to change this from xlsx to xlsm, or all of your work to the rest of the video is going to be thrown out the window. Save as, change the file type to xlsm or, heck, xlsb, either one of those will work. That's the one that is broken-- xlsx-- and it's the default, crazy isn't it? Xlsm, click Save. If you've never done macros before, Alt+T for Tom, M for Macro, S for Security and you will be able to save all macros without notification. Need to change that to the second one, that will allow your macros to work.

Alright, now we have two slicers. Bet you never knew this, but slicers have names. We're going to go to Slicer Tools, Options, Slicer Settings, and see this one is called Slicer_Name. Like that. Go to the second one, go to Slicer Tools, Options, Slicer Settings, this one's called Slicer_Name1-- not Name space 1, Name1. Two names like that.

Here's what we're going to do. We're going to switch over to VBA-- Alt+F11. In VBA, if you've never done VBA, you're going to have this big gray screen. We're going to come here and say View, Project Explorer, in the Project Explorer find your file-- mine's called Podcast 2104. Open Microsoft Excel Objects, and the sheet where I want this to work is called Dashboard. I'm going to right-click there and say View Code. This code that we're writing cannot go in a module like in a regular macro-- this has to be on this worksheet. Open the top left drop-down, Worksheet, then in the top right drop-down, we're going to say Pivot Table Update. Alright, so this is where our code's going to go now. I've already pre-baked this code. Let's take a look at the code here in notepad. So, we're going to have two Slicer caches-- SC1 and SC2-- one Slicer item and then, right here, this is where you're going to have to customize it. So my two Slicers were called Name and Name1. Alright, you're going to have to put your slicer names in there. Application.Screenupdating = False, Application.EnableEvents = False, and then Slicer Cache 2-- we're going to clear the filter, and then for each item SI1 and sc1.SlicerItems, if it's selected, then we're going to make the same item in Slicer Cache to be selected. This is a little loop that will run through however many items happen to be in that slicer. In my case, I have 11 or 12; in your case, you might have more.

When we're done with that, turn enable events back on, turn Screen Update back on. Alright. So, we'll take this code, copy this code and paste it here in the middle of our macro like that. Alright, now, let's just make sure I'm going to press Ctrl+G and my ask for is Application.EnableEvents, on or off-- so, ? Application.EnableEvents-- and it's true. If yours comes up as false, then you want to come back up here and say that it's = True-- so, then, you're turning those events on. Alright. Now, here's what's going to happen. So our coach should be working here, it's on the right worksheet. We're saved in an xlxm file, and I turned Macros on and what we're going to see, is that when I choose from the left Slicer, that Slicer Cache 1-- I'll choose Andy through Della-- the other Slicer is going to update as well. Alright And even if I would choose just Gloria-- just Gloria-- it looks like it's working really, really well. Even if I would CTRL+click, when I let go Ctrl, they all three will update.

But here's the gotcha-- there's always a gotcha-- this Slicer, it has to exist, but you cannot use this Slicer-- wait, I mean you can, you can use a Slicer but it's going to confuse the heck out of things. Because what's going to happen is I'm going to change this to Hank and they're going to go back to whatever is in Slicer Cache 1, because I changed the pivot table on this sheet. Now, in real life, are you going to have two pivot tables on the same sheet? I don't know if you are or if you aren't, alright, but things are going to become a little crazy.

Now, let's just take a look at this. First thing I want to do, is I'm going to insert a new worksheet-- Alt+IW for inserting the worksheet-- and I'm going to call this a DarkCave. You can call it whatever you want. I'm going to take that dashboard that's not going to work, I'm going to copy that dashboard and come here to the dark cave and paste it there and then right click and hide that sheet so no one ever sees that Slicer. And then, from here, we should be able to delete it. Nice, alright. And we're going to just check to make sure they're still working-- choose Charlie through Eddie and they're both still updating. Now, what's happening? The Slicer that we can't see, the one that we've hidden away, it's updating as well, but we don't care that it's updating.

Now, what if you want to have your things on different sheets? I'll insert a new worksheet here--Alt+IW-- and I'll take one of these pivot tables-- maybe the second pivot table-- and move it to that other sheet-- so, Ctrl+C to copy the pivot table, Ctrl+V to paste the pivot table here. And if I need to have a slicer here-- don't insert a slice from this pivot table-- we have to come back to our dashboard, take the slicer that's the controlling Slicer, Ctrl+C to make a copy of it, and paste it here-- Ctrl+V. Alright? Now, we have no code on this sheet-- there's no code on Sheet4-- and I was thinking I was going to have to add some code to Sheet4, but here's the beautiful thing: When I change this slicer, what's happening is, on the dashboard that pivot table's updating even though that pivot table on that sheet that's not active is updating, they will run the code and this will update as well. Pretty darn amazing that that works.

Now, the whole key to this is, you can never use the slicer tied to the second pivot table. You have to have the slicer that's tied to the second pivot table but you cannot use it-- you have to use this slicer tied to the first pivot table. Alright? But in general, I think this is working fairly well.

Alright, now hey, Sal, the person who asks this question, wrote in and said, "Look, I have a disconnected pivot table-- disconnected slicer in the second pivot table only." So let's just add a new field here called Region, East, West, we'll refresh our second pivot table, cool, and I'll insert a slicer that is disconnected-- in other words, it's only in the second data set, not in the first data set, Alright, now, this is going to be tricky because when I choose East from here, we're not going to have anybody selected. Alright? So the pivot table goes away. I would have to clear this slicer on the left hand side and then East remains selected. And now things have gone to hell, right? So, you are choosing from a slicer tied to the second data set and, while the second one is updating, the first one is not going to respect that because it has no idea there's no region filled back in the other field. This is only going to work when you have the same field in both data sets. If you have some other situation like this, then it will not fly.

So here's what you're going to have to do: You're going to have to insert that field-- the Region field-- back in your original data set, refresh this pivot table, insert a new slicer that will control that first pivot table. Alright? Now, we have two different slicers now, and because I built them backwards their names are backwards-- this one's Slicer_Region 1, and the one that's going to be the controlling one is called Slicer_Region2. If I would have planned differently, we would have had a different result, but here we are. Alt+F11, I want to take a lot of cutting and pasting. I'm going to take those first three lines and paste them, change it to SlicerCache3, SlicerCache4, SlicerItem3. I'll initialize SlicerCache3, ClicerCache4 to be Region2, Region1, clear the manual filter on SC4-- so that was a copy and paste, take this entire loop here and paste it. There are a lot of places you have to change-- your SI3, SC3 and then SC4, SI3. SI3-- don't miss that one, I missed that one-- next SI3. Alright, so now this set of code will hopefully control two sets of slicers. If you had a third set of slicers you're going to do the same changes I just made, copying and pasting and changing things carefully. Carefully. And again, now, this this guy is the one that we will never see-- we never want to see that one work-- because the ones on the Pivot Table 1 are the controlling ones. So this, we have to copy this-- Ctrl+C-- go to our sheet where we're hiding things away-- so Home, Format, Hide and Unhide, Unhide that sheet (the DarkCave), Paste so it continues to exist, it has to live somewhere, and then once I know it's back there on the DarkCave I can delete it and then hide this sheet here. Alright, and so now we should have on our dashboard, one set of slicers, we choose Central, they both update; we choose Just Flow, they both update; I clear the filter and Central stays. That's actually good. I'm glad that works-- clear this filter and everybody comes back. But these all have to be driving off that first pivot table. What if you have a field in the second data set that's not in your first data set? Then all bets are off. We'll go back to "I don't know how to solve that".

Well, hey, Macros came to the solution today and Macros are amazing and awesome. If you want to learn all about Macros, Tracy Syestad and I have written this great book, "Excel 2016, VBA and Macros." Check that out, Click the "I" on the top right hand corner to get to a page where you can buy that book.

Alright, Episode recap. How can you have a slicer drive two pivot tables? If they both came from the data set it's simple-- Slicer, Report Connections, Choose Other pivot tables. But if a pivot table came from two data sets, lots of steps change-- xlsx to xlsm, change your macro security setting, Alt+F11 to get the VBA, Ctrl+R to display the Project Explorer, find the worksheet name that contains your first pivot table and slicer, right-click and say View Code, and then Insert code for worksheet, Update, then, really important, Hide that second slicer away on a hidden worksheet or far out to the right so no one can ever choose from that slicer. By the way, don't cut that slicer-- you have to copy it and paste and then delete the first one in order to get it to work.

Want to thank you for stopping by, we'll see you next time for another netcast from MrExcel.

Download File

Download the sample file here: Podcast2104.xlsm

Title Photo: Engin_Akyurt / Pixabay


Bill Jelen is the author / co-author of
Excel Dynamic Arrays Straight to the Point

The new Dynamic Array Functions are just one side-effect of an effort to completely rewrite the Calculation Engine in Excel. Joe McDaid and the rest of the CALC team have the laid the groundwork for all future functions in Excel. Yes, the first crop of SORT, SORTBY, FILTER, UNIQUE, SEQUENCE and RANDARRAY are awesome and powerful, but they are just the first of many new functions that will come to Office 365 over the coming years.