Create Excel Pivot Table from Multiple Sheets

Pivot Table from Multiple Sheets

A common pivot table question is “How can I create a pivot table from data that’s on separate sheets in my workbook?

Sometime people have a workbook set up with a separate sheet for each region, or for each salesperson.

Eventually, they want to pull all the data together, and create a summary report in a pivot table, from multiple sheets.

Multiple Consolidation Ranges

Excel has a feature (well hidden Excel 2007) that lets you do this, using Multiple Consolidation Ranges.

A pivot table created this way has limited features, and isn’t much use in summarizing Excel data.

I usually recommend that you move all the data onto one worksheet, if it will fit, or store it in a table in Access, then use that as the source for the pivot table.

Create a Union Query

Another solution is to create a Union query from the separate tables, and use that as the source data.

Create a Union Query in Microsoft Query
Create a Union Query in Microsoft Query

Normal Pivot Table

With this solution, you’ll end up with a normal pivot table, with none of the limitations. However, it’s a bit tedious to set up, especially if you have more than a couple of tables.

Automate the Union Query

Instead of setting this up manually, you can use the code in a sample file from Excel MVPs, Kirill Lapin (KL), with amendments by Héctor Miguel Orozco Diaz. (You might remember Héctor’s innovative Filter Pivot Table Source Data example, posted earlier this year.)

To adjust their sample code to work in your file, you’d replace the sheet names in the CreateConnection code.

To go to the CreateConnection code, right-click on the “Create Empty Table” button, and click Assign Macro, then click Edit.

Pivot Table from Multiple Sheets 02

You can also adjust the location where the pivot table will be added. This line is further down in the CreateConnection code.

Pivot Table from Multiple Sheets 03

After those small changes, save the code changes. Then go back to Excel, click the button on the worksheet, and a summary pivot table will be automatically created.

Download the Sample File

Thanks Kirill and Héctor, for making a complicated task easier. You can download their sample file from the Contextures website: PT0023 – Pivot Table from Multiple Sheets

(Also, please check the update section below, for a newer version of the file)

UpdateDecember 2011

The solution described in this article was created as a conceptual prototype and targeted mainly advanced VBA users. The code has minimal error handling and compatibility checks.

Given the massive response from all kinds of users willing to adopt this solution in their own applications, we would like suggest a similar solution based on ADO.

Advantages:

  1. No need for temporary file generation
  2. The code is faster and less prone to errors

Disadvantages:

  1. No manual refresh of the PivotTable
  2. Need to rebuild connection from the scratch to update the cache with new data

Download the ADO Sample File

You can download the new ADO version of the file from the Contextures website: PT0024 – Pivot Table from Multiple Sheets – ADO version

Update — August 28, 2012

In the comments below, Kirill posted code that will automatically detect the sheet names. The blog formatting changed his minus sign to a long dash, and also deleted the Less Than Greater Than operator. Here is the correct code, with Kirill’s instructions:

In the code, replace this line:
' Sheets to consolidate
'*****************************************************************************
arrSheets = Array("310_BWATTS_P Pastujova", "310_BWATTS_Maria Sanchez")
'*****************************************************************************
with the following code:
' Sheets to consolidate
'*****************************************************************************
Dim ws As Worksheet
ReDim arrSheets(0)
For Each ws In ThisWorkbook.Worksheets
  If ws.Name <> ActiveSheet.Name Then
    arrSheets(UBound(arrSheets)) = ws.Name
    ReDim Preserve arrSheets(UBound(arrSheets) + 1)
  End If
Next ws
ReDim Preserve arrSheets(UBound(arrSheets) - 1)
'*****************************************************************************

________________

152 thoughts on “Create Excel Pivot Table from Multiple Sheets”

  1. Hi,
    I’ve been using the ADO version of the file and having a fantastic time with it.
    One issue I thought I’d raise is with the “Select Database Sheets” list. At this time, the size of the list is not dynamic and therefore the number of sheets that can be used is limited by the amount of screen real estate you have available.
    Is there any potential fix for this?
    Thanks in advance,
    Bill

    1. Solved this; very simple. Just have to adjust the zoom in the Userform line of the Createpivot code. If interested, see below…
      Sub CreatePivot()
      UpdateCache = False
      UserForm1.Zoom = 80
      UserForm1.Show
      End Sub

  2. Hi,
    I have been trying to use your Macro for multiple pivot sheets in Excel. I had deleted the sample data and inserted my own. In total there were 11 sheets, each with identical column headings, and only the row lengths on each of the sheets were different.
    When I run the Macro, I get the follwoing error: “All tables must have identical column names!”.
    Can you please help. I am new to all of this VBA script.
    I am happy to send across the file for you to have a look at.
    I am using the new ado file which was received from the following link: http://www.contextures.com/ExcelTemplates/VBA_PT_NormalMultipleSheets%20EN%2007.zip

  3. Is it possible to specify the area where the data is located?
    I want the data-area to be specified from coloumn 7 til 20, from row 30 til say 200
    Position is identical across the worksheet.
    Thanks for your help!
    Regards

  4. Thanks for your quick respons! No, it doesnt care. But if there is any other information in the sheet not belonging to the table – it all goes wrong as the code selects everything in the worksheet 🙂
    I was wondering if it was possible to limit this to a certain area. From SELECT * to something more precise. I tried to google other ways, without any luck. To bad as this code is perfect in every way except this!

  5. Yes, that’s certainly possible. What are the names of the columns you want to pull through? e.g. Age, Country, State etc. Also, what version of Excel do you have? IF 2007 or later, is the data in each sheet in Excel tables?

    1. I’m using excel 2010. There’s quite a few columns – but age, country, state is a example i can modify 🙂
      The data is not in a excel-table. But that could be fixed without major trouble. I think…

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.