简体   繁体   English

在 VBA 中使用 AdvancedFilter 时有没有办法使用“不等于”

[英]Is there a way to use “Not equal to” when using AdvancedFilter in VBA

My problem is quite simple, but I haven´t been able to find a solution that works anywhere.我的问题很简单,但我一直无法找到适用于任何地方的解决方案。

I want to filter out all values in one range that are present in another range - something like this:我想过滤掉一个范围内存在于另一个范围内的所有值 - 如下所示:

SheetA.Range("C1:C" & LastRowA).AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=<> & SheetB.Range("B1:B" & LastRow), Unique:=False

However, this does "<>" does not seem to work, as i get a syntax error.但是,这确实“<>”似乎不起作用,因为我收到语法错误。

Thanks in advance提前致谢

As you've discovered, Advanced Filter is only for filtering a range based on the values in another range - not for where they aren't present: https://docs.microsoft.com/en-us/office/vba/api/excel.range.advancedfilter正如您所发现的, Advanced Filter仅用于根据另一个范围中的值筛选范围 - 不适用于它们不存在的地方: https://docs.microsoft.com/en-us/office/vba/api /excel.range.advancedfilter

There are other ways using VBA to achieve this - for example, using autofilter and setting the filter condition.还有其他方法使用autofilter来实现这一点 - 例如,使用自动过滤器和设置过滤条件。 There are plenty of examples on SO that show how to do this though, one being here: https://stackoverflow.com/a/13936244/7858451 SO上有很多例子展示了如何做到这一点,一个在这里: https://stackoverflow.com/a/13936244/7858451

Just for fun, to try to get a work around where we absolutely are determined to use Advanced Filter to achieve what you want, there's a bit of a way round it to get the desired result...只是为了好玩,为了尝试解决我们绝对决心使用高级过滤器来实现您想要的工作,有一些方法可以解决它以获得所需的结果......

If you add an extra column to the right of your data to be filtered, adding a CountIF function there, something like COUNTIF(SheetB:$B$1,$B$100,$C2) (set the ranges accordingly, or better still - used named ranges) to determine whether that value is in the criteria range.如果您在要过滤的数据的右侧添加一个额外的列,请在此处添加一个 CountIF function ,例如COUNTIF(SheetB:$B$1,$B$100,$C2) (相应地设置范围,或者更好地 - 使用命名范围)来确定该值是否在条件范围内。

You then set a cell aside somewhere with the value 0 in it and use your advance filter to filter the countIF column for a match on the cell range containing the 0.然后,您将一个单元格放在一边,其中的值为 0,并使用您的高级过滤器过滤 countIF 列以匹配包含 0 的单元格范围。

This will then filter everything with a countIF of 0.这将过滤所有 countIF 为 0 的内容。

As I said, this is purely for fun, and not particularly practical - personally I would go down the autofilter route.正如我所说,这纯粹是为了好玩,而不是特别实用 - 我个人会autofilter沿着自动过滤路线走。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM