• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Count numbers separated by plus sign in formula

YasserKhalil

Well-Known Member
Hello everyone
I need to count the numbers in a formula that looks like that
Code:
=20+30+50
The numbers are separated by plus sign
I tried this UDF but it doesn't deal with formulas
Code:
Function COUNTTEXT(ref_value As Range, ref_string As String) As Long
    Dim I As Integer, Count As Integer
    Count = 0
    If Len(ref_string) <> 1 Then COUNTTEXT = CVErr(xlErrValue): Exit Function
        For I = 1 To Len(ref_value.Value)
            If Mid(ref_value, I, 1) = ref_string Then Count = Count + 1
        Next
    COUNTTEXT = Count
End Function
 
I tried this and it is ok
Code:
Function CountNumbers(Rng As Range)
    Dim Arr
    If Rng.HasFormula = True Then
        Arr = Split(Rng.Formula, "+")
    End If
    CountNumbers = UBound(Arr) + 1
End Function
 

I knew the Excel formula via Replace but I doubt than Yasser could catch it,
the reason why I told him to use Split and UBound functions
'cause he already knows how to use them …

Anyway, well done moroccan !
 
Back
Top