如果单元格包含范围,请计算数字
Count the numbers if a cell contains a range
我在 Google 电子表格上遇到了这个问题,但我知道电子表格和 Excel 很相似。
我用伪数据生成了这个 spreadsheet,但问题仍然存在。
我有一列,其中的行有一串单词,例如 "Apple/5",其中我想要的类别在“/”之前。 “/”后面的每个数字代表该类别的一个实例。所以 "Orange/2-3" 代表 2 个橙子,"Orange/6,7,8" 代表 3 个橙子,所以它会计算为 5 个橙子。
Excel/google 电子表格甚至可以做到这一点吗?
假设您要评估的文本在 A2 中:
=LEN(RIGHT(A2,LEN(A2)-FIND("/",A2)-LEN(SUBSTITUTE(SUBSTITUTE(RIGHT(A2,LEN(A2)-FIND("/",A2)),"-",""),",",""))+1))
在 Google 电子表格中不知道,但在 Excel 和 UDF 中,您可以在 1 列中提取您有多少水果,然后也许可以求和。对于这个答案,我使用了 UDF ONLYDIGITS designed by @paxdiablo,所以所有的功劳都归于他。
Public Function HOW_MANY(ByRef ThisCell As Range) As Long
If ThisCell.Count <> 1 Then 'If we select more than 1 cell, it won't work
HOW_MANY = ""
Else
HOW_MANY = Len(onlyDigits(ThisCell.Value))
End If
End Function
Private Function onlyDigits(s As String) As String
' Variables needed (remember to use "option explicit"). '
Dim retval As String ' This is the return string. '
Dim i As Integer ' Counter for character position. '
' Initialise return string to empty '
retval = ""
' For every character in input string, copy digits to '
' return string. '
For i = 1 To Len(s)
If Mid(s, i, 1) >= "0" And Mid(s, i, 1) <= "9" Then
retval = retval + Mid(s, i, 1)
End If
Next
' Then return the return string. '
onlyDigits = retval
End Function
我在 Google 电子表格上遇到了这个问题,但我知道电子表格和 Excel 很相似。
我用伪数据生成了这个 spreadsheet,但问题仍然存在。
我有一列,其中的行有一串单词,例如 "Apple/5",其中我想要的类别在“/”之前。 “/”后面的每个数字代表该类别的一个实例。所以 "Orange/2-3" 代表 2 个橙子,"Orange/6,7,8" 代表 3 个橙子,所以它会计算为 5 个橙子。
Excel/google 电子表格甚至可以做到这一点吗?
假设您要评估的文本在 A2 中:
=LEN(RIGHT(A2,LEN(A2)-FIND("/",A2)-LEN(SUBSTITUTE(SUBSTITUTE(RIGHT(A2,LEN(A2)-FIND("/",A2)),"-",""),",",""))+1))
在 Google 电子表格中不知道,但在 Excel 和 UDF 中,您可以在 1 列中提取您有多少水果,然后也许可以求和。对于这个答案,我使用了 UDF ONLYDIGITS designed by @paxdiablo,所以所有的功劳都归于他。
Public Function HOW_MANY(ByRef ThisCell As Range) As Long
If ThisCell.Count <> 1 Then 'If we select more than 1 cell, it won't work
HOW_MANY = ""
Else
HOW_MANY = Len(onlyDigits(ThisCell.Value))
End If
End Function
Private Function onlyDigits(s As String) As String
' Variables needed (remember to use "option explicit"). '
Dim retval As String ' This is the return string. '
Dim i As Integer ' Counter for character position. '
' Initialise return string to empty '
retval = ""
' For every character in input string, copy digits to '
' return string. '
For i = 1 To Len(s)
If Mid(s, i, 1) >= "0" And Mid(s, i, 1) <= "9" Then
retval = retval + Mid(s, i, 1)
End If
Next
' Then return the return string. '
onlyDigits = retval
End Function