如何使用 VBA 从 Excel 数据集自动创建过滤列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11364354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 16:46:49  来源:igfitidea点击:

How to automatically create filtered list from Excel dataset using VBA?

excelvbaexcel-vbaexcel-2007

提问by user1507035

I got a problem with Excel 2007 where I cannot find a solution to. I think one has to use VBA to do it by I am pretty unexperienced with that. I have a dataset in Excel (about 1k lines) with 10 columns. In an other tab/worksheet, I now want to display the data filtered by one column (country codes). It can be done dirty with autofiltering but it should be more like a report so it has to look good.

我在 Excel 2007 中遇到了问题,找不到解决方案。我认为必须使用 VBA 才能做到这一点,因为我对此非常缺乏经验。我在 Excel 中有一个数据集(大约 1k 行),有 10 列。在另一个选项卡/工作表中,我现在想显示按一列(国家/地区代码)过滤的数据。它可以通过自动过滤来完成,但它应该更像是一份报告,所以它必须看起来不错。

Let me make an example what I mean:

让我举个例子,我的意思是:

DATASET

数据集

Column | Column 2 | Column 3

专栏 | 第 2 栏 | 第 3 栏

Hans | GER | 16

汉斯 | 德国 | 16

Peter | US | 29

彼得 | 美国 | 29

Maria | GER | 30

玛丽亚 | 德国 | 30

Darian | US | 15

达里安 | 美国 | 15

Lisa | GER | 40

丽莎 | 德国 | 40

In an other Tab/Worksheet, I now want an automatically updated, formatted and filtered list based on this data set, e.g.:

在另一个选项卡/工作表中,我现在想要一个基于此数据集自动更新、格式化和过滤的列表,例如:

REPORT

报告

Germany

德国

Hans | 16

汉斯 | 16

Maria | 30

玛丽亚 | 30

Lisa | 40

丽莎 | 40

US

我们

Peter | 29

彼得 | 29

Darian | 15

达里安 | 15

I hope you understand what I mean. If I add new lines of data in my dataset sheet, I want the report to be automatically updated (so I can't just but autofilter lists below each other because the number of rows which will be needed depends on the data points). I think this should be pretty easy to do using a VBA macro, but I have no clue how to do it... Help very much appreciated!

我希望你明白我的意思。如果我在数据集表中添加新的数据行,我希望报告自动更新(所以我不能只是自动过滤彼此下面的列表,因为需要的行数取决于数据点)。我认为使用 VBA 宏应该很容易做到这一点,但我不知道该怎么做......非常感谢帮助!

Thanks upfront!

先谢谢了!

*

*

回答by Scott Holtzman

Have you tried a PivotTable combined with a dynamic named range?

您是否尝试过将数据透视表与动态命名范围相结合?

Given your data set above is set up in Sheet1 of your workbook, starting in A1 with column names. See the following solution.

鉴于上面的数据集是在工作簿的 Sheet1 中设置的,从 A1 开始,列名。请参阅以下解决方案。

1) Enter a dynamic named range for your dataset, like below.

1) 为您的数据集输入一个动态命名范围,如下所示。

enter image description here

在此处输入图片说明

This will ensure that any new data that is added to the end, or inserted into the middle of the data set gets captured and updated automatically with a pivot table refresh.

这将确保添加到末尾或插入到数据集中间的任何新数据都会通过数据透视表刷新自动捕获和更新。

2) Base your pivot table on the Named Range.

2) 将数据透视表建立在命名范围上。

enter image description here

在此处输入图片说明

3) Finally, set up your PivotTable like below.

3)最后,像下面一样设置你的数据透视表。

enter image description here

在此处输入图片说明

You can then format your pivot table how you like.

然后,您可以按照自己的喜好格式化数据透视表。

回答by SeanC

A Pivot table can do the type of grouping you are looking for. Using the data you provided, this is what I get from a pivot table:

数据透视表可以执行您正在寻找的分组类型。使用您提供的数据,这是我从数据透视表中得到的:

enter image description here

在此处输入图片说明

回答by Richard T

I suggest you take a look at pivot tables. they can be a bit complicated but once you figure out how they work they are very useful.

我建议你看看数据透视表。它们可能有点复杂,但是一旦你弄清楚它们是如何工作的,它们就会非常有用。