2014-02-14 46 views
1

我正在試圖在Excel VBA中查找一個計算單元格範圍內的數字數的公式。我嘗試了一個公式,但它只計算單個單元格內的數字數量。誰能幫我這個?計算單元格區域中的數字數

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

以及那就需要指定範圍的細胞。我認爲excel讓你幾乎自動地建立這些功能是很好的 – Coffee

回答

0

不明白什麼是需要和原因,但總結範圍A1:B2給定公式的結果,你可以使用一個數組公式是這樣的:

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

按住Ctrl + Shift + Enter鍵輸入公式作爲數組一個

+0

你的公式會給你帶有值'1 d a b'的單元格帶來什麼結果? –

+0

@simoco 4,但這不是我的公式(只有總和部分是)。對於來自圖片的Siddharth Rout輸入11 –

+0

SiddhartRout檢查值是否爲數字:'If IsNumeric(Trim(MYAr(i)))''但不是。我知道這是OP的公式,但應該提到它只適用於單元格只包含用空格分隔的數字的情況:) –

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

相關問題