计算一系列单元格中的数字数量

时间:2014-02-14 03:54:33

标签: excel excel-vba counting vba

我正在尝试在Excel VBA中找到一个公式,该公式将计算一系列单元格中的数字数。我尝试了一个公式,但它只统计单个单元格中的数字。任何人都可以帮我这个吗?

=IF(A1="",0,LEN(A1)-LEN(SUBSTITUTE(A1," ",""))+1) 

2 个答案:

答案 0 :(得分:2)

将此代码粘贴到模块中

Function GetNumbCount(Rng As Range) As Long
    Dim aCell As Range
    Dim MYAr As Variant
    Dim n As Long, i As Long

    For Each aCell In Rng
        If InStr(1, aCell.Value, " ") Then
            MYAr = Split(aCell.Value, " ")
            For i = LBound(MYAr) To UBound(MYAr)
                If IsNumeric(Trim(MYAr(i))) Then n = n + 1
            Next i
        Else
            If IsNumeric(Trim(aCell.Value)) Then n = n + 1
        End If
    Next

    GetNumbCount = n
End Function

然后在工作表中使用它

<强>语法

GetNumbCount(范围)

<强>截图

enter image description here

答案 1 :(得分:0)

无法理解需要什么以及为什么,但是要在A1:B2范围内对给定公式的结果求和,您可以使用这样的数组公式:

{=SUM(IF(A1:B2="",0,LEN(A1:B2)-LEN(SUBSTITUTE(A1:B2," ",""))+1))}

使用Ctrl + Shift + Enter将公式输入为数组

相关问题