天天看點

excel 宏 資料透視表_宏從多個檔案建立Excel資料透視表

excel 宏 資料透視表

If you want to create a pivot table from data on different worksheets, you can use a Multiple Consolidation Ranges pivot table. However, that creates a pivot table with limited features and functionality. Last year, Excel MVP Kirill Lapin (aka KL) shared his brilliant code to create a Union query and build a fully functional pivot table from data on different worksheets.

如果要根據不同工作表上的資料建立資料透視表,則可以使用“ 多個合并範圍”資料透視表 。 但是,這将建立具有有限特征和功能的資料透視表。 去年,Excel MVP Kirill Lapin(又名KL)分享了他出色的代碼,以建立Union查詢并根據不同工作表上的資料建構功能全面的資料透視表 。

Now, Kirill is back to share two more pivot table macros, and I'm sure you'll be impressed by both of them. In today's example, he's written a macro to create a pivot table from multiple Excel files.

現在,Kirill又回來分享了另外兩個資料透視表宏,我敢肯定,這兩個宏都會給您留下深刻的印象。 在今天的示例中,他編寫了一個宏,以從多個Excel檔案建立資料透視表。

This is based on a solution that Kirill posted in one of Russian-speaking Excel forums for generating a standard pivot table from multiple workbooks (as opposed to worksheets in the same workbook).

這是基于Kirill在講俄語的Excel論壇之一中釋出的一種解決方案的,該解決方案用于從多個工作簿(而不是同一工作簿中的工作表)生成标準資料透視表。

報告工作簿 (The Report Workbook)

The main file is named Report.xls, and it contains Kirill's pivot table code, and a button that runs the macro. When you open the file, enable the macros, then click the Create Pivot button.

主檔案名為Report.xls,它包含Kirill的資料透視表代碼和運作該宏的按鈕。 當您打開檔案時,啟用宏,然後單擊“建立資料透視”按鈕。

excel 宏 資料透視表_宏從多個檔案建立Excel資料透視表

資料檔案 (The Data Files)

When you run Kirill's macro, it prompts you to select one or more data files, all stored in the same folder. In this example, the files for Alberta, Ontrio and Yukon are selected.

運作Kirill的宏時,它會提示您選擇一個或多個資料檔案,所有檔案都存儲在同一檔案夾中。 在此示例中,選擇了Alberta,Ontrio和Yukon的檔案。

excel 宏 資料透視表_宏從多個檔案建立Excel資料透視表

All of the data files must have the same structure, and the macro works with the data stored on Sheet1 in each file. In the screen shot below you can see Sheet1 in the Alberta and Ontario files, which have identical column headings.

所有資料檔案都必須具有相同的結構,并且該宏可以處理每個檔案中存儲在Sheet1中的資料。 在下面的螢幕快照中,您可以在Alberta和Ontario檔案中看到Sheet1,它們具有相同的列标題。

excel 宏 資料透視表_宏從多個檔案建立Excel資料透視表

資料透視表 (The Pivot Table)

After you select the files and click Open, the macro creates a pivot table based on the data from Sheet1 in each of the selected files. Because the pivot table is based on a Union query, and not built from Multiple Consolidation Ranges, it's a fully functioning pivot table, and you can pivot, group and filter the data, as usual.

選擇檔案并單擊“打開”後,宏将基于每個標明檔案中Sheet1的資料建立資料透視表。 由于資料透視表基于聯合查詢,而不是根據多個合并範圍建構的,是以它是功能齊全的資料透視表,您可以照常對資料進行資料透視,分組和過濾。

You can also refresh the pivot table, to show the latest data in the source files, as long as those files stay in their original location.

您還可以重新整理資料透視表,以在源檔案中顯示最新資料,隻要這些檔案停留在其原始位置即可。

excel 宏 資料透視表_宏從多個檔案建立Excel資料透視表

聯合查詢 (The Union Query)

Kirill's macro creates a Union query to combine the data from all the selected files. If you open Microsoft Query, you can see the SQL string for the Union query, and all the data from the selected workbooks.

Kirill的宏建立了一個聯合查詢,以合并所有標明檔案中的資料。 如果您打開Microsoft Query,則可以看到Union查詢SQL字元串以及所選工作簿中的所有資料。

excel 宏 資料透視表_宏從多個檔案建立Excel資料透視表

下載下傳樣本檔案 (Download the Sample File)

To see Kirill's pivot table code, you can download the Pivot Workbooks example. The zipped folder that contains the Report.xls file, and the five sample data files. Unzip the folder, and keep all the files in the same folder. When you open the Report.xls file, enable macros to run the code.

要檢視Kirill的資料透視表代碼,可以下載下傳Pivot Workbooks示例 。 包含Report.xls檔案和五個示例資料檔案的壓縮檔案夾。 解壓縮該檔案夾,并将所有檔案保留在同一檔案夾中。 當您打開Report.xls檔案時,啟用宏以運作代碼。

資料透視表宏#2 (Pivot Table Macro #2)

Come back on Wednesday to see Kirill's second pivot table macro. It's another creative twist on creating a pivot table from data in different workbooks. ______________

周三傳回,檢視Kirill的第二個資料透視表宏。 這是根據不同工作簿中的資料建立資料透視表的另一種創造性的轉折。 ______________

翻譯自: https://contexturesblog.com/archives/2010/08/30/macro-creates-excel-pivot-table-from-multiple-files/

excel 宏 資料透視表