如何对数字系列的第一位数字求和

时间:2019-01-18 01:49:43

标签: excel vba

以下数字在单元格B5中开始:23 34 45 56 45 54 我想将每个数字的第一位数字相加,并在单元格C9中显示结果

我尝试了以下代码的不同调整。

Sub sum_first_digit()

    Dim colnum As Integer
    Dim sumfirst As Integer

    sumfirst = 0

    For colnum = 2 To 7 Step 1
        cellref = Cells(5, colnum)
        sumfirst = sumfirst + (Left(celref, 1))
    Next colnum

    Range("C9").Value = sumfirst


End Sub

期望单元格C9的数字为23。

实际是

  

运行时错误13:类型不匹配

7 个答案:

答案 0 :(得分:4)

我不知道为什么一个公式不能起作用。在C9中使用,

=SUMPRODUCT(--LEFT(B5:G5&0))
'an array formula that will skip over blank and text cells
=SUM(IFERROR(--LEFT(B5:G5&0), 0))

答案 1 :(得分:3)

第一位数的总和

功能

Function SFD(Range As Range) As Long
    Dim vnt As Variant, i As Long, j As Long
    vnt = Range
    For i = 1 To UBound(vnt)
        For j = 1 To UBound(vnt, 2)
            If IsNumeric(vnt(i, j)) Then
                If CLng(vnt(i, j)) > 0 Then SFD = SFD + CLng(Left(vnt(i, j), 1))
            End If
        Next
    Next
End Function

在C9单元格中,使用公式:=SFD(B5:G5)

阵列版本

Sub SumOfFirstDigit()

    Const cRow As Long = 5
    Const cCol1 As Variant = 2
    Const cCol2 As Variant = 7
    Const cTarget As String = "C9"

    Dim j As Long
    Dim sumFirst As Long
    Dim vnt As Variant

    vnt = Range(Cells(cRow, cCol1), Cells(cRow, cCol2))

    For j = 1 To UBound(vnt, 2)
        If IsNumeric(vnt(1, j)) Then
            If CLng(vnt(1, j)) > 0 Then _
                    sumFirst = sumFirst + CLng(Left(vnt(1, j), 1))
        End If
    Next

    Range(cTarget).Value = sumFirst

End Sub

范围版本

Sub SumOfFirstDigit2()

    Const cRow As Long = 5
    Const cCol1 As Variant = 2
    Const cCol2 As Variant = 7
    Const cTarget As String = "C9"

    Dim j As Long
    Dim sumFirst As Long

    For j = cCol1 To cCol2
        If IsNumeric(Cells(cRow, j)) Then
            If Cells(cRow, j) > 0 Then _
                    sumFirst = sumFirst + CLng(Left(Cells(cRow, j), 1))
        End If
    Next

    Range(cTarget).Value = sumFirst

End Sub

快速修复

  • 您忘记了Dim cellref As Range
  • 您在Set中忘记了Set cellref = Cells(5, colnum)
  • 您在celref中拼写了sumfirst = sumfirst + (Left(cellref, 1))
  • 您忘记了Set cellref = Nothing
  • Integerdead,以Long为准。

代码

Sub sum_first_digit()

    Dim cellref As Range
    Dim colnum As Long
    Dim sumfirst As Long

    sumfirst = 0

    For colnum = 2 To 7
        Set cellref = Cells(5, colnum)
        sumfirst = sumfirst + (Left(cellref, 1))
    Next colnum

    Range("C9").Value = sumfirst

    Set cellref = Nothing

End Sub

答案 2 :(得分:2)

我宁愿使用@ user10931127建议的公式,但是如果您仍然想要VBA代码,请尝试使用这一行VBA代码

Sub Sample()
    [C9] = [INDEX(SUM(VALUE(LEFT(A1:A6,1))),)]
End Sub

如果您想解释其工作原理,请参见THIS

答案 3 :(得分:1)

Sub sum_first_digit()

Dim Col As Long, X As Long

For Col = 2 To 7
    X = X + Left(Cells(5, Col), 1)
Next Col

Range("C9") = X

End Sub

答案 4 :(得分:1)

请尝试这个。

Sub sum_first_digit()

    Dim colnum As Integer
    Dim sumfirst As Integer
    Dim CelRef As Variant

    For colnum = 2 To 7
        CelRef = Left(Cells(5, colnum).Value, 1)
        sumfirst = sumfirst + Val(CelRef)
    Next colnum

    Range("C9").Value = sumfirst
End Sub

答案 5 :(得分:1)

问题出在变量 CellRef 中。首先,您用 2个字母L 书写,然后仅用 1个字母L

始终使用模块顶部的命令 Option Explicit 。它会强制您声明所有变量,并避免此类错误。

正确的代码:

Sub sum_first_digit()

    Dim colnum As Integer
    Dim sumfirst As Integer
    Dim cellref As Variant 'Change this type to another more specific (int, long or string)

    sumfirst = 0

    For colnum = 2 To 7 Step 1
        cellref = Cells(5, colnum)
        If IsNumeric(Left(cellref , 1)) Then sumfirst = sumfirst + Left(cellref , 1)
    Next colnum

    Range("C9").Value = sumfirst

End Sub

该错误是因为未声明变量** celref **,因此为空。左(“”,1)也为空。这样,您尝试在数字中添加文本,这就是类型不兼容的原因。

答案 6 :(得分:1)

尝试

sumfirst +(Left(celref,1))是字符串

sumfirst是整数

整数=字符串->错误类型不匹配

Sub sum_first_digit()

    Dim colnum As Integer
    Dim sumfirst As Integer

    sumfirst = 0

    For colnum = 2 To 7 Step 1
        cellref = Int(Cells(5, colnum) / 10)
        sumfirst = sumfirst + cellref
    Next colnum

    Range("C9").Value = sumfirst


End Sub