Excel Index + Match failing for single cell range

后端 未结 1 916
梦如初夏
梦如初夏 2021-01-07 09:22

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

相关标签:
1条回答
  • 2021-01-07 09:46

    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". 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.

    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, i.e.:

    {TRUE;FALSE}

    is an array, i.e. 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.

    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:

    =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.

    However, if we make a small change, e.g.:

    =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.

    This behaviour of MATCH is, in my opinion, inconsistent at best and a design fault at worst.

    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

    0 讨论(0)
提交回复
热议问题