英文:
Keeping score in a game and dealing with blank/non-numeric values
问题
我正在尝试制作一个用于桌面游戏的Excel记分表。
有两名玩家(红色和蓝色),每轮有九个段来记分。每个输入单元格要么有一个数字,一个或多个字母,两者都有,或者什么都没有。这些输入表示获得了多少分以及发生了什么操作。
示例1:记分表可能如下所示:
我想要“总计”列在每轮进行时保持每位玩家的分数总和。
示例2:如果上面的回合只进行到第4段,它将如下所示:
到目前为止,我可以从单个单元格中提取一个数字。数字将始终是第一个,且只有一个整数,因此我可以使用=LEFT(B2,1)
来获取数字1。
我还可以将这些数字串在一起,所以=LEFT(B2,1) + LEFT(C2,1) + LEFT(D2,1)
给我1 + 1 + 1 = 3
。
然而,如果字段为空(例如示例2中的F2:J2
)或者只包含一个字母(例如E2
和E3
),我会得到一个**#VALUE!
**错误。我认为这是有道理的,因为没有数字。那么我该如何解决这个问题?如何在一些输入为空或不包含数字的情况下获得累积总分?
我尝试使用=IFERROR(LEFT(A2,1),"0") + IFERROR(LEFT(B2,1),"0") + …
来用数字0替换任何错误,但如果输入为空或只包含一个字母,我仍然会得到#VALUE!错误。如果我删除引号,情况也是一样的=IFERROR(LEFT(A2,1),0) + IFERROR(LEFT(B2,1),0) + …
。
我还可以尝试什么?
英文:
I’m trying to make an Excel scoresheet for a tabletop game.
There are TWO players (red and blue) that have NINE segments in each round to score. Each entry will have either one number, one or more letters, both, or nothing. These entries represent how many points are scored and what action took place.
Example 1: The scoresheet might look like this
I want the TOTAL column to keep a running total of the points scored for each player as the round progresses.
Example 2: If the above round has only gone as far as Segment 4, it’ll look like this
So far, I can extract a number from a single cell. The number will always be first and only one integer, so I can use =LEFT(B2,1)
to get the number 1.
I can also string these together so =LEFT(B2,1) + LEFT(C2,1) + LEFT(D2,1)
gives me 1 + 1 + 1 = 3
.
However, if the field is blank (like F2:J2
in Example 2) or if it only contains a letter (like E2
and E3
) I get a #VALUE!
error. I think that makes sense because there’s no number. So how do I get around that? How do I get a running TOTAL if some entries are blank or don't contain a number?
I tried using =IFERROR(LEFT(A2,1),"0") + IFERROR(LEFT(B2,1),"0") + …
to replace any error with the number 0, but I still get the #VALUE! error if the entry is blank or only contains a letter. The same thing happens if I remove the quotes =IFERROR(LEFT(A2,1),0) + IFERROR(LEFT(B2,1),0) + …
What else can I try?
答案1
得分: 0
以下公式是一个数组公式,所根据您的Excel版本,您可能需要使用<kbd>CONTROL</KBD>+<kbd>SHIFT</KBD>+<kbd>ENTER</KBD>来确认您的公式。当您在公式栏中看到 {} 出现在公式周围时,就知道您已经做对了。{} 可能不需要手动输入。
=SUM(IF(ISNUMBER(--LEFT(B11:J11)),--LEFT(B11:J11),0))
将上述公式放入您的总计单元格中。根据需要调整范围。
注意:如果未指定LEFT或RIGHT的字母数量,默认为1。
如果您不想使用数组公式,您可以分别输入公式的每个部分,共输入9次,同时更改引用单元格。
如果您不想使用数组公式,您可以逐步输入公式的每个部分,每次更改引用单元格。
英文:
The following formula is an array formula so depending on your version of excel, you may need to confirm your formula using <kbd>CONTROL</KBD>+<kbd>SHIFT</KBD>+<kbd>ENTER</KBD>. You will know you have done it right when {} show up in the formula bar around the formula. The {} may not be entered manually
=SUM(IF(ISNUMBER(--LEFT(B11:J11)),--LEFT(B11:J11),0))
Place the above formula in your Total cell. Adjust the range as required.
NOTE: when the number of letters for LEFT or RIGHT is not specified, it defaults to 1.
If you do not want to use the array formula you may enter each part of the formula 9 time changing the reference cell as you go.
答案2
得分: 0
以下是使用SUM()
,IFERROR()
和LEFT()
的几种备选公式,如果您使用MS365
,还可以使用BYROW()
函数来返回动态填充的数组输出。
第一种备选公式:
• 单元格K2
中使用的公式
=SUM(IFERROR(--LEFT(B2:J2),0))
注意:
• 根据您的帖子
数字将始终是第一个且仅有一个整数,所以我可以使用=LEFT(B2,1)来获取数字1。
因此,我们可以使用LEFT()
函数,省略[num_chars]
,因为如果省略它,将被假定为1
,所以应用的函数是
LEFT(B2:J2)
• 接下来,我们应用了双重否定
--
来对上面的所有字符串操作进行数学运算,将文本作为#VALUE!
错误返回,为了解析它,我们使用了IFERROR()
函数。
IFERROR(--LEFT(B2:J2),0)
• 最后,我们将其包装在SUM()
内,以获得所需的输出。
=SUM(IFERROR(--LEFT(B2:J2),0))
• 还需要注意的是,如果您不使用MS365
,则在退出编辑模式并使用SUM()
函数时,根据Excel版本
,您可能需要按下<kbd>CTRL</kbd>+<kbd>SHIFT</kbd>+<kbd>ENTER</kbd>,但使用SUMPRODUCT()
函数可以避免这种情况。
第二种备选公式:
• 单元格L2
中使用的公式
=BYROW(B2:J2,LAMBDA(x,SUM(IFERROR(--LEFT(x),0))))
上述公式的工作方式相同,唯一的区别是它使用了BYROW()
函数,该函数将LAMBDA()
函数应用于数组中的每一行,并将每一行作为一个单一的数组公式返回。也就是说,它逐行迭代到LAMBDA()
函数,该函数使用SUM()
函数来返回每一行的总和。
英文:
Here are few alternative formulas using SUM()
& IFERROR()
with LEFT()
and if you are using MS365
can use BYROW()
function as well to return a dynamic spill array output.
First Alternative:
• Formula used in cell K2
=SUM(IFERROR(--LEFT(B2:J2),0))
Note:
• Since as per your post
> The number will always be first and only one integer, so I can use =LEFT(B2,1) to get the number 1.
Hence, we can use the LEFT()
function by omitting the [num_chars]
since if omitted it will be assumed as 1
so the function applied is
LEFT(B2:J2)
• Next, we applied double unary
or double negative
--
to do a math on the above and forces the results of all the string manipulation to number while the text will return error as #VALUE!
to parse we use the 0
with IFERROR()
function.
IFERROR(--LEFT(B2:J2),0)
• Lastly we wrap it within SUM()
to get desired output.
=SUM(IFERROR(--LEFT(B2:J2),0))
• Also, it has to be noted if you are not using MS365
then while exiting the edit mode and using SUM()
function you may need to hit the <kbd>CTRL</kbd>+<kbd>SHIFT</kbd>+<kbd>ENTER</kbd> based on Excel Versions
however using SUMPRODUCT()
function this can be avoided.
Second Alternative:
• Formula used in cell L2
=BYROW(B2:J2,LAMBDA(x,SUM(IFERROR(--LEFT(x),0))))
The above formula works in the same way only the difference is it uses the help of BYROW()
function which applies a LAMBDA()
function to each row in the array and results in per row as a single array formula. So that said it iterates each row one at a time to the LAMBDA()
function which uses the SUM()
function to return the total for each row.
答案3
得分: 0
这很简单:
=SUM(IFERROR(VALUE(LEFT(B2:J2,1)),0))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论