首页 > 解决方案 > 如何对数字序列的第一个数字求和

问题描述

以下数字从单元格 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:类型不匹配

标签: excelvba

解决方案


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

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

推荐阅读