Howdy folks. Jeff here, bringing you a Public Service Announcement: Thanks to the magic of VBA , Structured PivotTable References are coming to a PivotTable near you!
Structured References for PivotTables? So what? Well, because PivotTables are the best bit of ‘old’ Excel, and Tables are the best thing about ‘new’ Excel, and it’s about time their strengths were brought together:
- Tables magically expand to accommodate anything you put in them. Even better, because of the automated Dynamic Named Ranges built into Tables – called Structured Table References – any Formulas, Charts, Data Validation lists, or conditional formatting formulas that point to that table will instantly be updated with the latest data. And any PivotTables that point to that table will automatically include the new data whenever you refresh them. (Read more here).
- PivotTables allow you to do serious yet effortless number crunching without the need for a single formula. Just as well, because the kinds of formulas you need to replicate what a PivotTable can do easily are often mind-bogglingly complex, and very resource intensive. So using a PivotTable instead of formulas means that people that inherit your spreadsheet are less likely to struggle to follow what you’re doing, and the spreadsheet is less likely to suffer from slow recalculation issues. (Although yes, you will have to refresh that PivotTable from time to time. But that’s a small price to pay.) But there’s a problem with PivotTables: they don’t have any kind of inbuilt Dynamic Named Ranges like Tables do. And so because their structure is very likely to change whenever new data is added or a user decides to filter or rearrange the order or number of field displayed, then any formulas that point at PivotTable ranges will have to be changed manually. (With the exception of a single cell in the Data area referenced by the GETPIVOTDATA() function). So PivotTables are great for getting a result, but lousy for passing on those results to other parts of your spreadsheet.
Who knows why MS haven’t already implemeted Structured Referencing for PivotTables. But why wait for Microsoft to get around to it. Let’s just do it ourselves!
Download the sample file to see my hand-built Structured PivotTable Referencing in action: DynamicPivotRanges_20141019 unprotected
Open it, enable macros, and you’ll see a PivotTable like this:
Now, click on the arrow to the right of the name box, and you’ll see this:
As you can see, in my implementation of Structured PivotTable References, the automatic name that gets generated is prefixed with the Sheet name for uniqueness and uses a period to separate the Sheetname, PivotTable name, and FieldName. So it differs slightly from the notation that Tables use. But it’s every bit as handy.
For instance, check out what happens when I start typing a formula somewhere:
Awesome: That’s pretty much the same kind of thing I get when I want to reference a Table:
Let’s see if it handles changes in the structure of a PivotTable, shall we? Here, the Pivot is filtered in such a way that only 5 rows of data are returned. I’ve selected the entire State region, so that you can see that this corresponds with the automatically generated Structured PivotTable Reference shown in the Name box:
If I change the City filter to include additional cities, then the data returned grows by a few rows, as you can see below. Check out how the Structured PivotTable Reference automatically updated to accommodate the extra rows:
…and if I change the layout of the PivotTable by bringing in a new field – such as the BloodType field shown below – then as you can see, the Structured PivotTable Reference picks up the change too, and recognizes that the State field has shifted to the right:
If you change the Sheet name, then the SheetName part of the Structured PivotTable References syntax get updated next time the Pivot gets refreshed. And if you change the PivotName, then that part of the Structured PivotTable References syntax gets updated immediately. Unfortunately the same doesn’t occur for changes to PivotField names. So if you change the name of a field, any formulas pointing at the associated Structured Reference will need to be updated. This is shown below:
So there you have it: a proof-of-concept implementation of Structured PivotTable References. I’ve been using this to create complicated non-PivotCharts from Pivots, such as ScatterPlots (which are not supported in PivotCharts), or to serve up data labels to non-Pivot charts. And also to avoid having to have lots of extra formulas down the side of my PivotTable just to handle growth.
Take it for a spin, and let me know your thoughts and suggestions for improvements in the comments. Who knows…someone at Microsoft might even see this, and think “Now why didn’t WE think of that?”
What other functionality is missing from Excel that you’d like to see added?
While the things that Excel can do are cool, Excel often makes us jump through an awful lot of hoops – and click through an awful lot of dialog boxes – in order to actually do them. At the same time, there’s lots of things we routinely do that Excel simply doesn’t provide tools handy tools for. The end result is this: for every millisecond that Excel actually does some real work, we’ve probably spent hours ‘prepping’ it to do it.
Whenever we have to do lots of manual steps in order to leverage Excel’s cool inbuilt functionality, then Excel is programming us. It’s like some kind of epic experiment in behavioral psychology; and we’re the mice. It should be the other way around.
Fortunately, VBA (Macros) gives us the means to program Excel so that it behaves like we want it too. So if there’s something you would like to see added to Excel, let us know in the comments. We’ll see what we can collectively do to make Excel even greater than it already is!
—Edit—
My pal Doug Glancy actually wrote a post on how to do this back in 2012, on my birthday no less. I’d clean forgotten about that post. So be sure to check out Doug’s implementation of this too.
About the author
Yep, that’s me all right. Jeff Weird. Excel Madman.
If you liked this post, then you’ll love my upcoming book: Excel for Superheroes and Evil Geniuses. Keep a lookout for it in early 2015, and check out my posts on this blog or over at Daily Dose of Excel to get a feel for what kinds of things I’ll be covering. The book will give users an excellent overview of how Excel works under the covers, and what tools the interface puts at their fingers right out of the box. And it will ship with free code that will add amazing new features and functionality to Excel. You’ll be an Excel Evil Genius in no time!
Stay tuned…
One Response to “How to compare two Excel sheets using VLOOKUP? [FREE Template]”
Maybe I missed it, but this method doesn't include data from James that isn't contained in Sara's data.
I added a new sheet, and named the ranges for Sara and James.
Maybe something like:
B2: =SORT(UNIQUE(VSTACK(SaraCust, JamesCust)))
C2: =XLOOKUP(B2#,SaraCust,SaraPaid,"Missing")
D2: =XLOOKUP(B2#,JamesCust, JamesPaid,"Missing")
E2: =IF(ISERROR(C2#+D2#),"Missing",IF(C2#=D2#,"Yes","No"))
Then we can still do similar conditional formatting. But this will pull in data missing from Sara's sheet as well.