代码之家  ›  专栏  ›  技术社区  ›  jyp95

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

  •  -1
  • jyp95  · 技术社区  · 7 年前

    我在谷歌电子表格上有这个问题,但我知道电子表格和Excel是相似的。

    我生成了这个 spreadsheet 使用伪数据,但问题仍然是一样的。

    我有一个列,其中的行有一个字符串,例如“Apple/5”,其中我想要的类别在“/”之前。以“/”开头的每个数字表示该类别的一个实例。所以“Orange/2-3”代表2个桔子,“Orange/6,7,8”代表3个桔子,所以可以计算为5个桔子。

    Excel/谷歌电子表格是否可以做到这一点?

    2 回复  |  直到 7 年前
        1
  •  0
  •   Foxfire And Burns And Burns    7 年前

    在谷歌电子表格中不知道,但在Excel with和UDF中,你可以在一列中提取你有多少水果,然后可能得出你的总数。对于这个答案,我使用 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
    
        2
  •  0
  •   cybernetic.nomad    7 年前

    假设要计算的文本位于A2中: =LEN(RIGHT(A2,LEN(A2)-FIND("/",A2)-LEN(SUBSTITUTE(SUBSTITUTE(RIGHT(A2,LEN(A2)-FIND("/",A2)),"-",""),",",""))+1))