Need help with having excel 'count #s' with a twist

skonski

New Member
Joined
Jan 31, 2017
Messages
1
Hey Guys,

I'm trying to have excel count numbers and ranges of numbers all within one cell. I'm aware of =Len(A1) etc but that gives me a character count which isn't helpful.

So for example:


1, 3–5, 14–18, 20–26, 29, 30, 33, and 34


are all in one cell. I'd like excel to count the digits within the ranges as well as the individual numbers. So i'd like the function to calculate "20" in this case.


Any way to do this?

Thanks in advance.

Ryan
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Welcome to the forum.

Yes, it is possible, but it's highly susceptible to variations in how the data is entered. Here are two ways:

DEFG
11, 3–5, 14–18, 20–26, 29, 30, 33, and 34
2,1,3–5,14–18,20–26,29,30,33,34,1 ,3–5 ,14–18 ,20–26 ,29 ,30 ,33 ,and 34
35152020

<colgroup><col style="width: 25pxpx"><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet5

Worksheet Formulas
CellFormula
D2=","&SUBSTITUTE(SUBSTITUTE(D1," ",""),"and","")&","
E2=SUBSTITUTE(D1,", "," ,")
F3=D3+E3
G3=countnumbers(D1)

<thead>
</thead><tbody>
</tbody>

<tbody>
</tbody>

Array Formulas
CellFormula
D3{=SUM(IF(ISNUMBER(FIND(","&ROW(1:100)&",",D2)),1))}
E3{=SUM(IFERROR(MID(E2,FIND(","&ROW(1:100)&"–",E2)+LEN(ROW(1:100))+2,3)-ROW(1:100)+1,0))}

<thead>
</thead><tbody>
</tbody>
Entered with Ctrl+Shift+Enter. If entered correctly, Excel will surround with curly braces {}.
Note: Do not try and enter the {} manually yourself

<tbody>
</tbody>



Using just native Excel functions, I managed to get the proper answer. I used helper cells D2:E3, and the final sum is in F3. Yes, they can all be combined into a single formula, but it would be very long. It would also need a little cleaning up to make it a bit more robust, but as a proof of concept it shows that it's possible.

Option 2 would be to use a UDF. Press Alt-F11 to open the VBA editor, from the menu select Insert > Module, and paste this code:

Code:
Public Function CountNumbers(ByVal MyList As String) As Long
Dim MyNums As Variant, x As Variant, wk As Variant

    MyList = Replace(MyList, " ", "")
    MyList = Replace(MyList, "and", "")
    MyList = Replace(MyList, ChrW(8211), "-")
    MyNums = Split(MyList, ",")
    
    For Each x In MyNums
        wk = Split(x, "-")
        If UBound(wk) = 0 Then
            CountNumbers = CountNumbers + 1
        Else
            CountNumbers = wk(1) - wk(0) + 1 + CountNumbers
        End If
    Next x
            
End Function
Then press Alt-Q to exit the editor, and use the formula in G3. Much neater. This is also prone to unexpected formatted data, but it's much better than the native formulas.

Let me know if this helps.
 
Upvote 0

Forum statistics

Threads
1,216,487
Messages
6,130,944
Members
449,608
Latest member
jacobmudombe

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top