天天看点

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 宏 数据透视表