具有某些文本字段的列的总和

时间:2018-10-25 16:53:53

标签: excel excel-formula

查看我是否可以获取enter列的总和,包括具有符号和数字的列。但是带有符号的实际上是文本字段。

2
1
- → 2
3

有什么想法吗?我已经尝试了一些其他方法,包括使用RIGHT,但仍将其视为文本并且未添加。

2 个答案:

答案 0 :(得分:1)

尝试以下用户定义功能:

Public Function Zum(rng As Range) As Variant
    Dim r As Range, s As String, L As Long, i As Long
    Dim numbr As String, CH As String

    Zum = 0
    For Each r In rng
        s = r.Text
        If s <> "" Then
            L = Len(s)
            numbr = ""
            For i = 1 To L
                CH = Mid(s, i, 1)
                If CH Like "[0-9]" Or CH = "." Then
                    numbr = numbr & CH
                End If
            Next i
            If numbr <> "" Then
                Zum = Zum + CDbl(numbr)
            End If
        End If
    Next r
End Function

例如:

enter image description here

它将添加单元格,但是在执行添加操作之前,请从单元格中删除除数字和小数点以外的所有内容。

EDIT#1:

此版本的代码仅处理每个单元格的最右位数字

忽略字母和小数点:

Public Function Zum(rng As Range) As Variant

'   version #2  only sum the right-most digits and ignore the decimal point


    Dim r As Range, s As String, L As Long, i As Long
    Dim numbr As String, CH As String

    Zum = 0
    For Each r In rng
        s = r.Text
        If s <> "" Then
            L = Len(s)
            numbr = ""
            For i = L To 1 Step -1
                CH = Mid(s, i, 1)
                If CH Like "[0-9]" Then
                    numbr = CH & numbr
                Else
                    Exit For
                End If
            Next i
            If numbr <> "" Then
                Zum = Zum + CDbl(numbr)
            End If
        End If
    Next r
End Function

,如果只需要最右边的单个数字

Public Function Zum(rng As Range) As Variant

'   version #3  only sum the right-most digit and ignore the decimal point


    Dim r As Range, s As String, L As Long, i As Long
    Dim numbr As String, CH As String

    Zum = 0
    For Each r In rng
        s = r.Text
        If s <> "" Then
            CH = Right(s, 1)
            If CH Like "[0-9]" Then
                Zum = Zum + CLng(CH)
            End If
        End If
    Next r
End Function

答案 1 :(得分:0)

这需要您将其作为助手列一直向下滑动,但您可以在其后将其累加。假定开头处最多有一个字符,然后在所需的数字前有空格和箭头。让我知道这是否不是您所期望的。

=IF(MID(A11,3,1)="→", --RIGHT(A11,LEN(A11)-4),A11)

这可能更可靠,只要第二个数字最多为2位数字 =IF(ISERR(SEARCH("→",A1)),A1,--RIGHT(A1,2))

编辑:这应该处理非值问题,但是Scott的答案似乎更接近您想要的结果。我不确定您如何修改他以使其考虑到非值单元格。

=IF(ISNUMBER(A4),A4,IF(MID(A4,3,1)="→", IFERROR(--RIGHT(A4,LEN(A4)-4),"")),"")