英文:
I cant get the last number that is not 0 from a selected area of columns
问题
我无法从所选列区域中获取最后一个不为0的数字。
我无法从E22:E52获取最后一个不等于0的数字。
这些列是货币类型,包含正数和负数。
英文:
I can't get the last number that is not 0 from a selected area of columns.
I can't get the last number that is not =0 from E22:E52.
The columns are currency typed are both positive and negative numbers.
答案1
得分: 2
变量
r
🠚 源范围
n
🠚 ISNUMBER() <> 0
公式
- LET函数用于将计算结果存储在变量中以供以后使用。使用它是可选的,但可以使公式更短,也许更容易理解。
- FILTER函数用于从范围**
r
中返回不等于零的数字数组n
** - INDEX返回由对**
n
**的所有行进行COUNT计算得出的最后一行。
使用LET的公式
=LET(r,E22:E52,
n,FILTER(r,ISNUMBER(r)*(r<>0)),
INDEX(n,COUNT(n)))
没有LET的公式
=INDEX(FILTER(E22:E52,ISNUMBER(E22:E52)*(E22:E52<>0)),
COUNT(FILTER(E22:E52,ISNUMBER(E22:E52)*(E22:E52<>0))))
字符串过滤
上述公式中的FILTER函数不会返回字符串,因为其中一个条件使用了ISNUMBER。
如果数据包括'数字字符串'
要接受与数字格式匹配的字符串,例如我的示例图像中的A18
,必须首先将这些字符串转换为数字。
- 可以在ARRAYFORMULA中对范围
r
内的所有值使用VALUE函数,或使用不会更改数字值的任何数学操作来完成此操作。例如,VALUE(r), r+0, r*1, r/1, r*(1/2+1/2)
等。
- 结果数组将包含数字和
#VALUE!
错误(无法转换为数字的值)。 - 潜在的错误对于FILTER函数来说不是问题,对于任何测试值解析为错误的条件,它都会返回FALSE,除非条件明确寻找错误。例如,
FILTER({0;1;1/0;2},{0;1;1/0;2}<>0)={1;2}
FILTER({0;1;1/0;2},{0;1;1/0;2}=0)={0}
FILTER({0;1;1/0;2},NOT(ISERROR({0;1;1/0;2}))={0;1;2}
FILTER({0;1;1/0;2},ISERROR({0;1;1/0;2})={#DIV/0!}
- ISNUMBER检查变得不再必要,因为现在所有的值都是数字或错误。
=LET(r,ARRAYFORMULA(E22:E52*1),
n,FILTER(r,r<>0),INDEX(n,ROWS(n)))
# 或
=LET(r,ARRAYFORMULA(VALUE(E22:E52)),
n,FILTER(r,r<>0),INDEX(n,COUNT(n)))
如果数据绝不包括字符串
如果您的数据范围绝不包含文本字符串,那么您可以通过去除ISNUMBER检查来简化公式。
=LET(r,E22:E52, n,FILTER(r,r<>0),
INDEX(n,COUNT(n)))
# 或
=INDEX(FILTER(E22:E52,E22:E52<>0),
COUNT(FILTER(E22:E52,E22:E52<>0)))
英文:
Variables
r
🠚 Source Range
<br>
n
🠚 ISNUMBER() <> 0
Formula
- The LET function enables storing calculations in variables for later re-use. Using it is optional, but makes the formula shorter and perhaps easier to follow.
- The FILTER function is used to return an array of numbers
n
not equal to zero from the ranger
- INDEX returns the last row in
n
which is calculated from a COUNT of all the rows inn
.
Formula using LET
=LET(r,E22:E52,
n,FILTER(r,ISNUMBER(r)*(r<>0)),
INDEX(n,COUNT(n)))
Formula without LET
=INDEX(FILTER(E22:E52,ISNUMBER(E22:E52)*(E22:E52<>0)),
COUNT(FILTER(E22:E52,ISNUMBER(E22:E52)*(E22:E52<>0))))
[<img src="https://i.stack.imgur.com/bztIb.png" width="350" />](https://i.stack.imgur.com/bztIb.png "click to enlarge")
String Filtering
The FILTER function in the formulas above won't return strings because one of its conditions uses ISNUMBER.
If Data Includes 'Number Strings'
To accept strings that match number formats, A18
in my example images, those strings must first be coerced to numbers.
- This can be done to all values in the range
r
inside an ARRAYFORMULA with the VALUE function, or using any math operation that would 'not' change the value of a number. For example,VALUE(r), r+0, r×1, r÷1, r×(½+½)
, etc.
- The resulting array will be populated by numbers and
#VALUE!
errors (values that could not be coerced to numbers) - The potential errors are not a problem for the FILTER function, which will return FALSE for any condition where the tested value resolves to an error, except if the condition is explicitly looking for an error. For example,
FILTER({0;1;1/0;2},{0;1;1/0;2}<>0)={1;2}
FILTER({0;1;1/0;2},{0;1;1/0;2}=0)={0}
FILTER({0;1;1/0;2},NOT(ISERROR({0;1;1/0;2}))={0;1;2}
FILTER({0;1;1/0;2},ISERROR({0;1;1/0;2})={#DIV/0!}
<br>
- the ISNUMBER check becomes unnecessary since all values are now numbers or errors.
=LET(r,ARRAYFORMULA(E22:E52*1),
n,FILTER(r,r<>0),INDEX(n,ROWS(n)))
# or
=LET(r,ARRAYFORMULA(VALUE(E22:E52)),
n,FILTER(r,r<>0),INDEX(n,COUNT(n)))
[<img src="https://i.stack.imgur.com/sWgkQ.png" width="350" />](https://i.stack.imgur.com/sWgkQ.png "click to enlarge")
If Data NEVER Includes Strings
If there are NEVER any values in your data range that are text strings then you can simplify the formula by removing the ISNUMBER check
=LET(r,E22:E52, n,FILTER(r,r<>0),
INDEX(n,COUNT(n)))
# or
=INDEX(FILTER(E22:E52,E22:E52<>0),
COUNT(FILTER(E22:E52,E22:E52<>0)))
答案2
得分: 1
你也可以尝试:
=LET(r,E22:E52,INDEX(r,MATCH(,ROW(r)*(r=0))))
或者:
=CHOOSEROWS(FILTER(E22:E52,E22:E52),-1)
第一个公式使用MATCH
进行近似查找,找到不满足条件E22:E52=0
的最后一行的相对位置,然后使用INDEX
返回相应的值。
第二个公式过滤掉E22:E52中的所有非零值,然后使用CHOOSEROWS(...,-1)
返回最后一个值。
英文:
You could also try:
=LET(r,E22:E52,INDEX(r,MATCH(,ROW(r)*(r=0))))
Or:
=CHOOSEROWS(FILTER(E22:E52,E22:E52),-1)
The first formula uses MATCH
with an approximate lookup to find the relative position of the last row that doesn't respect the condition E22:E52=0
and then uses INDEX
to return the corresponding value.
The second formula filters out all the non zero values in E22:E52 and then uses CHOOSEROWS(...,-1)
to return the last one.
答案3
得分: 0
以下是翻译好的部分:
=INDEX(FILTER(E22:E52;E22:E52<>0); COUNT(FILTER(E22:E52;E22:E52<>0)))
或者
=INDEX(FILTER(E22:E52,E22:E52<>0), COUNT(FILTER(E22:E52,E22:E52<>0)))
请注意,;
变成了 ,
英文:
One of these will work (depending on your locale)
=INDEX(FILTER(E22:E52;E22:E52<>0);
COUNT(FILTER(E22:E52;E22:E52<>0)))
OR
=INDEX(FILTER(E22:E52,E22:E52<>0),
COUNT(FILTER(E22:E52,E22:E52<>0)))
Notice the ;
turning to ,
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论