excel/vba - 在列中查找特定值的第一次和最后一次出现

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

excel/vba - find fist and last occurrence of a particular value in a column

excelvbaexcel-formula

提问by bsapaka

So if I have a column such as:

因此,如果我有一个专栏,例如:

    A1
1   Apple
2   Apple
3   Apple
4   Oj
5   Oj
6   Oj
7   Oj
8   Pear
9   Pear

How could I return the values 1 & 3 for Apple, 4 & 7 for OJ, etc?

我如何为 Apple 返回值 1 和 3,为 OJ 返回值 4 和 7,等等?

回答by barry houdini

Formula-wise you can use MATCHfunctions, e.g. for first Apple position

公式方面,您可以使用MATCH函数,例如第一个 Apple 位置

=MATCH("Apple",A1:A9,0)

=MATCH("Apple",A1:A9,0)

for last

最后

=MATCH(2,INDEX(1/(A1:A9="Apple"),0))

=MATCH(2,INDEX(1/(A1:A9="Apple"),0))

or if the fruit are sorted as per your example (or merely grouped) you can get the last by adding the number of apples to the first -1

或者如果水果按照您的示例排序(或仅分组),您可以通过将苹果的数量添加到第一个 -1 来获得最后一个

so with first MATCH function in C1 that would be

所以在 C1 中的第一个 MATCH 函数将是

=COUNTIF(A1:A9,"Apple")+C1-1

=COUNTIF(A1:A9,"Apple")+C1-1