简体   繁体   English

AdvancedFitler 使用公式从 ListRange 输出值

[英]AdvancedFitler Out Values from ListRange using Formula

I'm trying to setup AdvancedFilter to filter out a ListRange of items.我正在尝试设置 AdvancedFilter 以过滤出 ListRange 项目。 After some testing, I realized that it only accepts using a "formula" of <>A when I use a criteria range of 2 cells.经过一些测试,我意识到当我使用 2 个单元格的标准范围时,它只接受使用<>A的“公式”。 If I add a third <>B it just does nothing.如果我添加第三个<>B它什么也不做。

My original thought was simple to prepend to my column <> to each cells value, but now it seems that won't work.我最初的想法很简单,可以将列<>添加到每个单元格的值,但现在看来这行不通。 I need to figure out a way to have both a formula and a range somehow applied.我需要想办法以某种方式同时应用公式和范围。

IE: IE:

Data:数据:

Let Num
A   1
B   2
C   3

This Works for Filter Range:这适用于过滤器范围:

Let
<>B

This Doesn't:这不会:

Let
<>B
<>C

But my CriteriaRng looks like this:但我的 CriteriaRng 看起来像这样:

Let
B
C

How I can reference a way to say for all items in Let column, Filter <>Cell.Value in CriteriaRange:=我如何引用一种方式来表示Let列中的所有项目,Filter <>Cell.Value in CriteriaRange:=

Here's the basic code I tried/debugged this issue with:这是我尝试/调试此问题的基本代码:

 FilterRng.AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range("D1:D3"), Unique:=False
 Stop
 FilterRng.AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range("D1:D2"), Unique:=False
 Stop

Updates:更新:

I found this example --> https://www.mrexcel.com/board/threads/with-adavnced-filter-how-do-i-exclude-a-value.733153/page-2我找到了这个例子——> https://www.mrexcel.com/board/threads/with-adavnced-filter-how-do-i-exclude-a-value.733153/page-2

=ISNA(MATCH($A9,Exclude!$A$1:$A$2,0))

But I'd need to built that formula via VBA and make it much more generic.但我需要通过 VBA 构建该公式并使其更加通用。 I'm better w/ VBA then formula's.我比 VBA 更好,然后是公式。

I also read in this post that he basically uses highlighting via regular filter, then another filter based on highlighting, but I know there is a better way utilizing a formula in a cell.我还在这篇文章中读到,他基本上通过常规过滤器使用突出显示,然后是另一个基于突出显示的过滤器,但我知道在单元格中使用公式有更好的方法。

https://stackoverflow.com/a/34012365/5079799 https://stackoverflow.com/a/34012365/5079799

I think I also somewhere you can do "or" operations when staggering rows w/ advanced filter, so I could make my column a staggered column, but that also sounds hacky and I couldn't get it to work on my brief attempt.我想我也可以在使用高级过滤器交错行时执行“或”操作,所以我可以使我的列成为交错列,但这听起来也很奇怪,我无法让它在我的短暂尝试中工作。

If you have multiple lines in your Criteria you're doing an OR operation.如果您的 Criteria 中有多行,则您正在执行 OR 操作。 If you want to do an AND operation you need a single line in your criteria but the same Caption listed multiple times, see below.如果您想执行 AND 操作,您的条件中需要一行,但多次列出相同的 Caption,请参见下文。 在此处输入图像描述

If you name your ranges: Database, Criteria, and Extract respectively then record a macro and run the advanced filter it will write the code for you.如果您分别命名您的范围:数据库、标准和提取,然后记录一个宏并运行高级过滤器,它将为您编写代码。 You can then modify the code to accept variable input.然后,您可以修改代码以接受变量输入。

I basically copied my answer from this one, but built the FormulaStr and automated it more, as thats the point of VBA!我基本上从这个答案中复制了我的答案,但是构建了FormulaStr并使其更加自动化,这就是 VBA 的重点!

https://stackoverflow.com/a/28506854/5079799 https://stackoverflow.com/a/28506854/5079799

Sub Test()

Dim ws As Worksheet
Set ws = ActiveSheet

Dim CriteriaRng As Range, DataRng As Range
Set CriteriaRng = ws.Range("D1:D3")
Set DataRng = ws.Range("A1:B4")

Dim FormulaRng As Range, FormulaStr As String, DataRngCellTwoStr As Range
Set DataRngCellTwoStr = Cells(DataRng.Row + 1, DataRng.Column)
Set FormulaRng = ws.Range(Cells(2, CriteriaRng.Column + 1), Cells(2, CriteriaRng.Column + 1))
FormulaStr = "=ISNA(MATCH(" & DataRngCellTwoStr.Address(False, False) & "," & CriteriaRng.Address & ",0))"
FormulaRng.Value = FormulaStr
ws.Range(Cells(1, CriteriaRng.Column + 1), Cells(1, CriteriaRng.Column + 1)).Clear
Set FormulaRng = ws.Range(Cells(1, CriteriaRng.Column + 1), Cells(2, CriteriaRng.Column + 1))

DataRng.AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=FormulaRng, Unique:=False

End Sub

Notes:笔记:

  • You must enter Formula on 2nd row and make FilterRng exactly 2 rows!您必须在第 2 行输入公式,并使 FilterRng 正好为 2 行!
  • The Header Should be BLANK Header 应为空白
  • Formula should looks like this =ISNA(MATCH(A2,$D$1:$D$3,0)) with A2 being first row below headers of criteria column in filter range and D1:D3 being the criteria column.公式应如下所示=ISNA(MATCH(A2,$D$1:$D$3,0)) ,其中 A2 是过滤器范围内标准列标题下方的第一行,而 D1:D3 是标准列。

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

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