C# - Excel - 如何获取可用于 autofilter() 方法的条件

发布于 2024-09-28 20:00:52 字数 693 浏览 0 评论 0原文

我想使用 autoFilter() 方法来过滤某些行。
我已成功地在选定范围(整个工作表)上激活 autoFilter() 方法,没有任何问题。
我在选定范围上使用的方法如下所示:

range.AutoFilter(5, myCriteria, Excel.XlAutoFilterOperator.xlAnd, missing, true);

其中:

5 是列索引,
myCriteria 是用于过滤的对象。

当我在调用该方法之前知道 myCriteria 是什么时,这种方法就可以正常工作。

但是,如果我不知道要使用什么条件,并且我想从特定列(例如 #5 列)中的可用值中获取它,该怎么办

请注意,当您在 Excel 中使用自动筛选器时手动,您会得到一个包含可用选项的下拉菜单。
例如,如果有很多行,则该列 #5 可能具有 0、5、10、15、20 之类的值,但我无法提前知道这一点。

困难的方法是迭代该列中的所有值并构建唯一值的列表。

有没有一种快速且简单的方法可以做到这一点?

(我必须使用 Excel 2003 和“Microsoft Excel 11.0 对象库”)

谢谢

I want to use the autoFilter() method to filter some rows.
I've managed to activate the autoFilter() method on a selected range (the whole sheet) with no problem.
The method i use on a selected range looks like this:

range.AutoFilter(5, myCriteria, Excel.XlAutoFilterOperator.xlAnd, missing, true);

where:

5 is the column index,
myCriteria is the object to filter with.

This works fine when I know what myCriteria is before I call the method.

But what if I don't know what criteria to use, and I want to get it from the values available in a particular column (column #5 for example)

Note that when you use auto filter in Excel manually, you get a drop-down menu which contains the available choices.
For example, if there are many rows, that column #5 may have values like 0, 5, 10, 15, 20, but I can't know this in advance.

The hard way will be to iterate through all the values in that column and build a list of unique values.

Is there a fast and easy way to do it?

(I have to use Excel 2003 with "Microsoft Excel 11.0 Object Library" )

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不语却知心 2024-10-05 20:00:52

您可以以编程方式使用高级过滤器< /a> 与 唯一仅记录 已勾选,但这会在一张纸上为您提供一个列表。

然而,如果你仔细想想,这到底有没有道理呢?为什么您想要使用该列表中的随机值来过滤列表?

如果用户想要进行选择,他们将提供一个值供您进行过滤。
如果您想以编程方式过滤某些记录,那么您已经知道过滤条件是什么。如果不这样做,您就不必过滤该列表。

You can programatically use advanced filter with Unique Records Only ticked, but that will give you a list on a sheet.

However, if you think about it, does it make sense at all? Why would you ever want to filter a list using a random value from this very list?

If the user wants to make a selection, they will provide a value for you to filter with.
If you want to filter some records programmatically, then again, you already know what your filter condition is going to be. And if you don't, you don't have to filter the list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文