简体   繁体   English

Excel 索引 + 单个单元格范围的匹配失败

[英]Excel Index + Match failing for single cell range

I am looking to try and find the first negative number in a list that can contain one or more values.我希望尝试在可以包含一个或多个值的列表中找到第一个负数。 I am using Index/Match.我正在使用索引/匹配。 When the range (or named referenced) has two or more cells, it find the value correctly.当范围(或命名引用)具有两个或多个单元格时,它会正确找到该值。 However, when the range has only one cell, it fails.但是,当范围只有一个单元格时,它会失败。

eg Excel data in column K1 and K2 are -10.00 and 20.00, respectively例如,K1 和 K2 列中的 Excel 数据分别为 -10.00 和 20.00

When I try below, it returns -10.00 as expected当我在下面尝试时,它按预期返回 -10.00

=INDEX(K1:K2, MATCH(TRUE,K1:K2<0,0),)

However, when I search only one cell range, it fails with "#N/A".但是,当我只搜索一个单元格范围时,它会失败并显示“#N/A”。 I would expect it to return -10.00.我希望它返回-10.00。

=INDEX(K1:K1, MATCH(TRUE,K1:K1<0,0),)

The same issues occurs when using named reference that contain a single cell.使用包含单个单元格的命名引用时会出现同样的问题。

Many thanks非常感谢

It's a very interesting question, and one which requires quite a technical answer. 这是一个非常有趣的问题,并且需要一个非常技术性的答案。

Basically, MATCH requires that the lookup_array be of a certain "type". 基本上,MATCH要求lookup_array是某个“类型”。 This is not to say that this array cannot contain just a single element (it can), but rather that it be of a form which is compatible with that which MATCH is expecting to be passed for its second parameter. 这并不是说该数组不能仅包含一个元素(它可以包含),而是它的形式与MATCH期望为其第二个参数传递的形式兼容。

And this acceptable form can be either an array of values or a reference to a (contiguous) one-dimensional range of worksheet cells. 这种可接受的形式可以是值的数组,也可以是对(连续)一维工作表单元格范围的引用。

When you perform: 执行时:

=INDEX(K1:K2,MATCH(TRUE,K1:K2<0,0),)

this resolves to: 解析为:

=INDEX(K1:K2,MATCH(TRUE,{TRUE;FALSE},0),)

and so the lookup_array , ie: 因此是lookup_array ,即:

{TRUE;FALSE}

is an array, ie of an acceptable form. 是一个数组,即具有可接受的形式。

However, when you perform: 但是,当您执行:

=INDEX(K1:K2,MATCH(TRUE,K1:K1<0,0),)

then this resolves to: 然后解决为:

=INDEX(K1:K2,MATCH(TRUE,TRUE,0),)

and this time the lookup_array (TRUE) is a single Boolean value, which does not meet the requirements imposed upon the type for this parameter. 并且这次lookup_array (TRUE)是单个布尔值,不满足对此参数的类型施加的要求。

We can, however, artificially coerce that single value so that it is of the correct type, viz: 但是,我们可以人为地强制该单个值,使其具有正确的类型,即:

=INDEX(K1:K2,MATCH(TRUE,IF({1},K1:K1)<0,0),)

which this time resolves to: 这次解析为:

=INDEX(K1:K2,MATCH(TRUE,{-10})<0,0),)

which is: 这是:

=INDEX(K1:K2,MATCH(TRUE,{TRUE},0),)

and now that the single Boolean is technically part of an array (albeit one which contains only a single value), the above resolves as required. 现在,从技术上讲,单个布尔值是数组的一部分(尽管其中仅包含一个值),以上内容可以按需解决。

As another example of this behaviour, imagine that cell A1 contains the value 1. Then: 作为此行为的另一个示例,假设单元格A1包含值1。然后:

=MATCH(1,A1,0)

correctly returns 1, since, even though the lookup_array is here a single cell, it is (as all worksheet ranges are) still technically of an array-type. 正确返回1,因为,即使lookup_array在这里是单个单元格,但从技术上讲,它(因为所有工作表范围都是)仍然是数组类型。

However, if we make a small change, eg: 但是,如果我们做一些小改动,例如:

=MATCH(1,N(A1),0)

then, even though this resolves as: 然后,即使此解析为:

=MATCH(1,1,0)

and so it would appear that nothing at all has changed, in fact, by "dereferencing" the range reference A1 to its actual value, we have now made it of an invalid type to pass as the lookup_array , the above thus resulting in an error. 因此,似乎没有任何改变,实际上,通过将范围引用A1“解引用”为其实际值,我们现在使它成为无效类型,以作为lookup_array传递,上述结果导致错误。

This behaviour of MATCH is, in my opinion, inconsistent at best and a design fault at worst. 我认为,MATCH的这种行为最好是不一致的,而最坏的情况是设计错误。

The result of: 的结果:

=INDEX(K1:K2,MATCH(TRUE,K1:K1<0,0),)

should be: 应该是:

=INDEX(K1:K2,MATCH(TRUE,{TRUE},0),)

without us having to coerce it via additional, artificial means. 无需我们通过其他人为手段强制实施。

For some reason, if the array being passed comprises just a single value, then this value is first "resolved" into a non-array-type, unlike with those arrays which consist of more than one value, for which their array-type is retained. 出于某种原因,如果要传递的数组仅包含一个值,则该值首先被“解析”为非数组类型,与那些由多个值组成的数组不同,其数组类型为保留。

Regards 问候

I came across the same issue with lookup_array and luckily ran into this thread.我在lookup_array上遇到了同样的问题,幸运的是遇到了这个线程。

I really like the explanation by @XOR LX.我真的很喜欢@XOR LX 的解释。 However, the proposed conceptual solution IF({1},K1:K1) is not usable in Excel.然而,提出的概念解决方案IF({1},K1:K1)在 Excel 中不可用。

So the final formula that worked for me was:所以对我有用的最终公式是:

MATCH(
  range,
  IF(
    COUNTA(lookup_array)=1,
    CHOOSE({1,2},lookup_array,""),
    lookup_array
  ),
  0
)

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

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