Assume a workbook with two worksheets – Jan and Feb. In both the worksheets, the following exist
1. Same number of columns on both worksheets; and
2. Same order of columns on both worksheets; and
3. Same spellings of headings on both worksheets
As you can observe in this workbook, all three aspects mentioned above are in place. One may want to perform any of the following tasks:
1. Create one pivot table from both worksheets; and/or
2. Consolidate data from both worksheets (one below the other)
To curtail the workbook size, I have deleted rows of data. In the actual workbook, there were 65,000 rows of data on each worksheet.
While one simple way would be to copy and paste data from both worksheets into one and then create a pivot table, the following shortcomings exist with this method
1. Copying and pasting data from multiple worksheets into one is a manual process; and
2. Since the pivot table will be based on the manually created sheet (by copying and pasting), changes in the two original sheets will not update the pivot table on refreshing unless the same changes are carried out in the consolidated sheet (created by copying and pasting above) as well. So this leads to duplication of effort.
Both problems outlined above can be resolved by using MS Query. The technique mentioned below will work as long as the three conditions mentioned above are satisfied. Furthermore, since MS Query will only recognize named ranged with rows up to 65,536, the number of rows of data in each of the worksheets should not exceed 65,536. The combined rows in both worksheets could be any number.
Depending upon the version of MS Excel which you are using, there could be two ways to solve this problem.
Solution for MS Excel 2010 and higher versions
If you are using the Power Query add-in, then a few simple steps (no formulas at all) can solve this problem. The result will be dynamic and refreshable (just as in a Pivot Table). In Power Query, there is a feature to append data from multiple worksheets into one running range. Once this is done, the running range can either be transferred to an MS Excel worksheet (if the number or rows are up to 1 million) from where a Pivot Table can be created OR the running range can be loaded to the Data Model (Excel 2013) [the Data Model will be able to accommodate more than 1 million rows depending upon the hardware and software configuration of the machine] from where a Pivot Table can be created.
Solution for all versions of MS Excel
You may watch a short video of my solution here
The steps for creating a pivot table from multiple worksheets (both in the same file) are:
1. Suppose the workbook with the two worksheets (Jan and Feb) is named Monthly data.xls and is saved on the desktop.
2. Select the data on the Jan sheet (including the first row as the header row – on the Jan sheet, it will be A1:S4. Ensure that the header row has some distinctive formatting such as Bold or some colour) and press Ctrl+F3 > New. In the Name box, type Dummy and click on OK > Close.
3. To cross check that the name assigned above has indeed been assigned correctly, select the data range once again and in the Name box (left of the formula bar), Dummy should appear.
4. Select A1:S4 of the Jan sheet again and press Ctrl+T to convert this range into a Table. Ensure that the “My Table has headers” box is checked.
5. Repeat steps 2 – 4 for the Feb sheet as well. In step 3, just change the name to Dummy1
6. Save the workbook as Consolidated.xls (save as Excel workbook if the combined rows in Dummy and Dummy1 exceed 65,536 rows) and close it
7. Open a new workbook (Ctrl+N) and go to Data > From Other Sources > From Microsoft Query
8. Under Databases, select Excel files > OK
9. In the Directories dialog box, navigate to the folder on the desktop where the Monthly Data.xls file is saved. So for me, it is saved under C:\Users\Ashish\Desktop\ and double click on the folder where Monthly Data.xls file is saved
10. In the left hand side window, select the Monthly Data file and click on OK
11. With Dummy selected, click on the > symbol to bring over all columns of this named range to the right hand side box
12. Select Dummy1 and click on the > symbol to bring over all columns of this named range to the right hand side box
13. Click on Next and on the message box which appears, click on OK
14. Click on the SQL button and delete all content from the SQL Statement box
15. Type this SQL statement in the box there
Select * from dummy
union all
Select * from dummy1
16. Click on OK and on the message box which appears, click on OK
17. Under File, select the last option – Return Data to Microsoft Excel
18. At this stage, if you wish to get data from Dummy and Dummy1, one below the other, then select Table. If you directly want a pivot table, select the second option button – Pivot Table. Select any cell where you would like to the result to appear, say cell A1.
19. Click on OK. A counter will run at the bottom left hand side with the title of Reading Data
20. If you had selected pivot table in step 18 above, then the pivot table grid/layout will appear.
21. You may now drag fields to create a pivot table
22. Close the workbook
Update Table/Pivot Table for addition/editing in existing named ranges
To see the effect of a dynamic Table/Pivot Table, edit data in Jan and/or Feb sheets of Monthly data.xls. Save and close the file. Open Consolidated.xls and refresh the Table/Pivot Table (Right click and Refresh). At the bottom right had side the counter will run again and (step 19 above) and once it has read all data, the Table/Pivot Table will update.
Furthermore, since the two named ranges (Dummy and Dummy1) have been converted into Tables, even if you add data by rows (with no row being left entirely blank), when you right click to Refresh the Table/Pivot Table, data of new rows will appear.
Update Table/Pivot Table for addition of new worksheets
Assume you now add a new worksheet, titled March in Monthly data.xls. To include this sheet in the pivot Table, follow the under mentioned steps:
1. Open Monthly data.xls
2. Follow steps 2 – 4 mentioned above for the March sheet. In step 3, just change the name to Dummy2
3. Save and close the workbook
4. Open Consolidated.xls
5. Select any cell in the pivot table and under Pivot Table Tools (red button in the MS Excel title bar) > Options, click on the small drop down arrow under Refresh and select Connection Properties. If you selected Table (instead of Pivot Table) in step 18 above, click on any cell in the Table and under Table Tools (yellow button in the MS Excel title bar) > Options, click on the small drop down arrow under Refresh and select Connection Properties.
6. Click on the Definition tab and under Command Text, add the following at the end of the SQL query:
union all
Select * from dummy2
7. When you click on OK, the counter will run again and the pivot should reflect data from March sheet.
Update Table/Pivot Table when files are mailed to someone else
If you now mail both files – Monthly Data.xls and Consolidated.xls to someone else, that other person will not be able to refresh the pivot table because the path specified in step 9 above would be different for that other person. To reestablish the path, follow the under mentioned steps:
1. Open Consolidated.xls
2. Select any cell in the pivot table and under Pivot Table Tools (red button in the MS Excel title bar) > Options, click on the small drop down arrow under Refresh and select Connection Properties. If you selected Table (instead of Pivot Table) in step 18 above, click on any cell in the Table and under Table Tools (yellow button in the MS Excel title bar) > Options, click on the small drop down arrow under Refresh and select Connection Properties.
3. Click on the Definition tab and under Connection String, edit the following:
a. From DBQ= till the next ;, change the file path to the new path where the mail recipient has saved the file. So, on my system, between DBQ= and the next ;, the path is C:\Users\Ashish\Desktop\Jan.xls. This will change to C:\Users\John\Desktop\Data\Jan.xls
b. From DefaultDir= till next ;, change the folder path to the new path where the mail recipient has saved the file. So, on my system, between DefaultDir= and the next ;, the path is C:\Users\Ashish\Desktop\. This will change to C:\Users\John\Desktop\Data\
Create a Pivot Table from multiple worksheets in the same workbook
{ 328 Comments }