简体   繁体   English

Excel #VALUE:根据计算的起始值、结束值使用 INDEX、INDEX 或 SEQUENCE 从范围中选择子集时出错

[英]Excel #VALUE! error when selecting a subset from a range using INDEX : INDEX or SEQUENCE based on calculated start, end values

This is an hypothetic sample to show the following Excel odd behavior in several situations, where a subset of a range is selected via INDEX:INDEX or INDEX and SEQUENCE , where start and end positions of the subset of the range or the sequence are calculated via INDEX or FILTER .这是一个假设示例,用于显示以下几种情况下的 Excel 奇怪行为,其中通过INDEX:INDEXINDEX and SEQUENCE选择范围的子集,其中范围子集或序列的startend位置通过以下方式计算INDEXFILTER

The scenario is the following: I have an input range with values: {1;2;3;4} in the range: A1:A4 and I want to sum the range from 2 to 4 with the expected result 9 , but to illustrate the point, I am going to calculate such numbers checking even condition.场景如下:我有一个输入范围,其值: {1;2;3;4}在范围内: A1:A4我想将24的范围与预期结果9相加,但为了说明重点是,我将计算这些数字以检查偶数条件。 The following formula considers several use cases:以下公式考虑了几个用例:

  • CaseA , CaseC : Obtain the subset via INDEX(a,start):INDEX(a, end) where start and end are calculated field and a refers to the input range. CaseA , CaseC : 通过INDEX(a,start):INDEX(a, end)获取子集,其中startend为计算字段, a为输入范围。
  • CaseB , CaseD : Obtain the subset via INDEX(a, SEQUENCE(rows,, start) where rows and start are calculated fields and a refers to the input range. CaseB , CaseD :通过INDEX(a, SEQUENCE(rows,, start)获取子集,其中rowsstart是计算字段, a是指输入范围。

As part of my research and trying to find workarounds I use two ways for calculating the start and end of the subset:作为我研究的一部分并试图找到解决方法,我使用两种方法来计算子集的startend

  • Using INDEX represented with names with the suffix X .使用以带有后缀X的名称表示的INDEX
  • Not using INDEX represented with names with the suffix Y .不使用以名称后缀Y表示的INDEX For example XLOOKUP or FILTER .例如XLOOKUPFILTER

Here is the formula that covers four scenarios via VSTACK/HSTACK to compare all of them in a single call:以下是通过VSTACK/HSTACK涵盖四种情况的公式,可在一次调用中比较所有情况:

=LET(a, A1:A4, even, FILTER(a, MOD(a,2)=0),
    startX, INDEX(even, 1), endX, INDEX(even,2),
    startY, XLOOKUP(2, even, even), endY, FILTER(even, even > startX),
    caseA, IFERROR(SUM(INDEX(a,startX):INDEX(a, endX)), "ERROR"),
    caseB, IFERROR(SUM(INDEX(a, SEQUENCE(endX-startX+1,, startX))), "ERROR"),
    caseC, IFERROR(SUM(INDEX(a,startY):INDEX(a, endY)), "ERROR"),
    caseD, IFERROR(SUM(INDEX(a, SEQUENCE(endY-startY+1,, startY))),"ERROR"),
    VSTACK({"caseA", "caseB", "caseC", "caseD"}, 
      HSTACK(caseA, caseB, caseC, caseD))
  )

It returns an error: #VALUE!它返回一个错误: #VALUE! or a wrong result.或错误的结果。 If I wrap FILTER with MIN , which is not really necessary because FILTER returns a single value , it works:如果我用MIN包装FILTER这并不是真正必要的,因为FILTER返回单个值,它可以工作:

excel输出

Similarly if I wrap startX , endX with MIN it also works.同样,如果我用MIN包装startXendX ,它也可以工作。

Notes :注意事项

  • Using A2:A4 on every where I use the name a , provides the same result.在我使用名称a每个地方使用A2:A4提供相同的结果。
  • Using IFERROR to prevent VSTACK/HSTACK returns a single value in case of error.使用IFERROR防止VSTACK/HSTACK在发生错误时返回单个值。

Am I doing something wrong or is it a possible bug?我做错了什么还是可能是错误?

I think it would be of benefit to both you and those wishing to help you if you constructed simpler examples to illustrate your points.如果您构建更简单的示例来说明您的观点,我认为这对您和那些希望帮助您的人都有好处。 What's more, use of the Evaluate Formula tool might help guide you towards an explanation.此外,使用Evaluate Formula工具可能有助于指导您进行解释。

There is no bug here, and the behaviour you are witnessing has always been present in Excel.这里没有错误,您看到的行为一直存在于 Excel 中。

With the same entries as yours in A1:A4 , I will use the following simplified example to explain what is happening:使用与A1:A4中相同的条目,我将使用以下简化示例来解释正在发生的事情:

=INDEX(H1:H10,FILTER(A1:A4,A1:A4=1))

Looking at the Evaluate Formula tool, you can see that this resolves to:查看Evaluate Formula工具,您可以看到这解析为:

=INDEX(H1:H10,{1})

which is not technically identical to这在技术上与

=INDEX(H1:H10,1)

and suggests that FILTER is returning an array , albeit one comprising only a single value.并建议FILTER返回一个数组,尽管它只包含一个值。

For functions which are being instructed to return a cell value , there is no issue in this array being passed.对于被指示返回单元格值的函数,传递此数组没有问题。 This is the case with the example above, which will not error.上面的例子就是这样,不会报错。

However, if we extend it to:但是,如果我们将其扩展为:

=SUM(INDEX(H1:H10,FILTER(A1:A4,A1:A4=1)):INDEX(H1:H10,FILTER(A1:A4,A1:A4=1)))

then, by employing INDEX in this way, we are asking it to return a cell reference , not a cell value , ie we are asking然后,通过以这种方式使用INDEX ,我们要求它返回一个单元格引用,而不是一个单元格值,即我们要求

=SUM(INDEX(H1:H10,{1}):INDEX(H1:H10,{1}))

to be interpreted as被解释为

=SUM(H1:H1)

though such constructions do not allow arrays.尽管这样的结构不允许 arrays。

OFFSET and INDIRECT are two other functions which can return cell references and which exhibit similar behaviour. OFFSETINDIRECT是另外两个可以返回单元格引用并表现出类似行为的函数。

=OFFSET(H1,1,0)

and

=INDIRECT("H"&1)

are valid, whereas是有效的,而

=OFFSET(H1,{1},0)

and

=INDIRECT("H"&{1})

are not.不是。

Of course, in all these cases the single-value array can be coerced into a non-array via some function (eg SUM , MIN ).当然,在所有这些情况下,单值数组都可以通过一些 function(例如SUMMIN )强制转换为非数组。

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

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