vba 使用excel中的内置函数查找列中的最后一个匹配项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23205575/
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-12 02:50:35  来源:igfitidea点击:

find last match in column using built in functions in excel

excelexcel-vbaexcel-formulavba

提问by dwstein

I have an excel workbook with multiple instances of the same client ID. So, a client can appear more than once in the list. I would like to find the last (closest to the bottom) instance of the client ID so i can then look along that same row for additional information. VLOOKUP shows the fist instance, I need the last.

我有一个 Excel 工作簿,其中包含相同客户端 ID 的多个实例。因此,一个客户端可以多次出现在列表中。我想找到客户端 ID 的最后一个(最接近底部)实例,以便我可以沿着同一行查看其他信息。VLOOKUP 显示第一个实例,我需要最后一个。

So, how do I find the last instance of a given client ID using built in functions? I'd rather not write a macro to do this.

那么,如何使用内置函数找到给定客户端 ID 的最后一个实例?我宁愿不写一个宏来做到这一点。

回答by Dmitry Pavliv

Suppose you want to find last instance of id "id_1"in range A2:A8and return corresponding value from range B2:B8, then use:

假设您想"id_1"在 range 中找到 id 的最后一个实例A2:A8并从 range 返回相应的值B2:B8,然后使用:

=LOOKUP(2,1/(A2:A8="id_1"),B2:B8)

enter image description here

在此处输入图片说明



If you want to return indexof last intance of id "id_1"in range A2:A8, use:

如果要返回range中 id 的最后一个实例的索引,请使用:"id_1"A2:A8

=MATCH(2,1/(A2:A8="id_1"))

with array entry (CTRL+SHIFT+ENTER).

与阵列条目(CTRL+ SHIFT+ ENTER)。

If you want to return row numberof last intance of id "id_1", use:

如果要返回id 的最后一个实例的行号"id_1",请使用:

=LOOKUP(2,1/(A2:A8="id_1"),ROW(A2:A8))

This syntax is a special trick: (A2:A8="id_1")evaluates to {TRUE,FALSE,FALSE,TRUE,...}. Next, assuming that TRUE=1and FALSE=0, the part 1/{TRUE,FALSE,FALSE,TRUE,...}gives you {1,#DIV0!,#DIV0!,1,..}.

这种语法是一个特殊的技巧:(A2:A8="id_1")计算为{TRUE,FALSE,FALSE,TRUE,...}。接下来,假设TRUE=1FALSE=0,该部分1/{TRUE,FALSE,FALSE,TRUE,...}为您提供{1,#DIV0!,#DIV0!,1,..}

To return last number in resulting array lookup_valueshould be greater than any value in lookup_array ({1,#DIV0!,#DIV0!,1,..}in our case). Since array contains only 1or #DIV0!, we can take 2as lookup_value, because it's always greater than 1.

返回结果数组中的最后一个数字lookup_value应该大于 lookup_array{1,#DIV0!,#DIV0!,1,..}中的任何值(在我们的例子中)。由于数组只包含1or #DIV0!,我们可以取2as lookup_value,因为它总是大于1