Counting consecutive numbers in a comma-separated list

mrc44

Board Regular
Joined
Aug 12, 2017
Messages
64
I tried to find a formula or a macro for this online but I couldn't find anything.

I need a way to calculate the consecutive numbers in a comma separated list. Thanks in advance.

Here is a sample data: ConsecutiveCount.xlsm

I'm using Excel 2016.

* In the sample file, I put the expected results in separate cells, but it can be in a single too.
 
VBA Code:
Sub test()
    Dim al, lst, son&, i&, ii&, topla%, fark
    son = Cells(Rows.Count, 1).End(3).Row
    Range("B2:I" & Rows.Count).ClearContents
    ReDim lst(2 To son, 1 To 8)
    For i = 2 To son
        topla = 0
        al = Split(Cells(i, 1).Value & ",9999999", ",")
        For ii = 1 To UBound(al)
            fark = Val(al(ii)) - Val(al(ii - 1))
            If fark = 1 Then
                topla = topla + 1
            Else
                If topla > 0 And fark <> 1 Then
                    lst(i, topla) = lst(i, topla) + 1
                    lst(i, 8) = lst(i, 8) + 1
                    topla = 0
                End If
            End If
        Next ii
    Next i
    Range("B2").Resize(son - 1, 8).Value = lst
End Sub
 
Last edited:
Upvote 0

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college

Forum statistics

Threads
1,216,099
Messages
6,128,822
Members
449,469
Latest member
Kingwi11y

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