如果单元格包含范围,则计算数字

时间:2018-02-20 23:45:04

标签: excel spreadsheet

我在Google电子表格中遇到此问题,但我知道电子表格和Excel类似。

我使用伪数据生成了此spreadsheet,但问题仍然存在。

我有一个列,其中的行有一串字,例如“Apple / 5”,其中我想要的类别在“/”之前。进行“/”的每个数字代表该类别的实例。因此“橙色/ 2-3”代表2个橙子,“橙色/ 6,7,8”代表3个橙色,因此它将计算为5个橙子。

这是否可以使用Excel / google电子表格?

2 个答案:

答案 0 :(得分:0)

假设您要评估的文字是A2: =LEN(RIGHT(A2,LEN(A2)-FIND("/",A2)-LEN(SUBSTITUTE(SUBSTITUTE(RIGHT(A2,LEN(A2)-FIND("/",A2)),"-",""),",",""))+1))

答案 1 :(得分:0)

不知道在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
相关问题