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.
 
D3 is also incorrect, there is only 1 set of 4 consecutive numbers, not 4 sets.

This is what I think you want, if it is not correct then you will need to provide a more accurate representation for us to work with.
I've set it out with individual counts as in your example, then added an extra column at the end which gives just the total of consecutive streaks from the row (which I think is what you were asking for in post 1). This formula is independent of the others so columns B:G could be deleted and the total would still work.

9-12-20.xlsx
ABCDEFGHI
1DATA2 Consecutives3 Consecutives4 Consecutives5 consecutives6 consecutives7 consecutives8 consecutivesAll
21,2,6,8,9,14,182      2
31,2,3,4  1    1
45,10,11,16,18,201      1
53,4,5,11,12,21,2311     2
610,13,14,15,16,171      1
Sheet1
Cell Formulas
RangeFormula
B2:H6B2=IFERROR(1/(1/SUM(--(FREQUENCY(IF(ISNUMBER(SEARCH(","&ROW(B$1:B$14)&",",","&$A2&",")),ROW(B$1:B$14)),IF(ISERROR(SEARCH(","&ROW(B$1:B$14)&",",","&$A2&",")),ROW(B$1:B$14)))=COLUMN(B2)))),"")
I2:I6I2=IFERROR(1/(1/SUM(--(FREQUENCY(IF(ISNUMBER(SEARCH(","&ROW(I$1:I$14)&",",","&$A2&",")),ROW(I$1:I$14)),IF(ISERROR(SEARCH(","&ROW(I$1:I$14)&",",","&$A2&",")),ROW(I$1:I$14)))>1))),"")
Press CTRL+SHIFT+ENTER to enter array formulas.
 
Upvote 0

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Thanks. You understood me correctly. I checked your formula, it works but it doesn't give the correct result for the last cell (A6).

ConsecutiveCount.xlsm
ABCDEFGH
1DATA2 Consecutives3 Consecutives4 Consecutives5 consecutives6 consecutives7 consecutives8 consecutives
21,2,6,8,9,14,182
31,2,3,41
45,10,11,16,18,201
53,4,5,11,12,21,2311
610,13,14,15,16,171
Sheet1
 
Last edited:
Upvote 0
Not sure why but the limit was set to 14 instead of 20
9-12-20.xlsx
ABCDEFGHI
1DATA2 Consecutives3 Consecutives4 Consecutives5 consecutives6 consecutives7 consecutives8 consecutivesAll
21,2,6,8,9,14,182      2
31,2,3,4  1    1
45,10,11,16,18,201      1
53,4,5,11,12,21,2311     2
610,13,14,15,16,17   1   1
Sheet1
Cell Formulas
RangeFormula
B2:H6B2=IFERROR(1/(1/SUM(--(FREQUENCY(IF(ISNUMBER(SEARCH(","&ROW(B$1:B$20)&",",","&$A2&",")),ROW(B$1:B$20)),IF(ISERROR(SEARCH(","&ROW(B$1:B$20)&",",","&$A2&",")),ROW(B$1:B$20)))=COLUMN(B2)))),"")
I2:I6I2=IFERROR(1/(1/SUM(--(FREQUENCY(IF(ISNUMBER(SEARCH(","&ROW(I$1:I$20)&",",","&$A2&",")),ROW(I$1:I$20)),IF(ISERROR(SEARCH(","&ROW(I$1:I$20)&",",","&$A2&",")),ROW(I$1:I$20)))>1))),"")
Press CTRL+SHIFT+ENTER to enter array formulas.
 
Upvote 0
Solution
Here is a way using Power Query.

Book2
ABCDEFGHI
1DATA2345678Total
21,2,6,8,9,14,1822
31,2,3,411
45,10,11,16,18,2011
53,4,5,11,12,21,23112
610,13,14,15,16,1711
71,2,3,4,5,6,8,10,1211
81,3,5,6,7,8,9,10,1111
91,2,3,5,6,7,8,9,10,11,12112
Sheet2


Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    Split = Table.TransformColumns(Source,{{"DATA", each Table.AddIndexColumn(Table.FromList(Text.Split(_,",")),"Index",0,1)}}),
    Consec = Table.TransformColumns(Split,{{"DATA", (tbl)=> Table.AddColumn(tbl,"Custom",(row)=>  try Number.From((Number.From(tbl[Column1]{row[Index]+1}) - Number.From(row[Column1]))=1) otherwise 0 ) }}),
    Group = Table.TransformColumns(Consec, {{"DATA", (tbl)=> Table.TransformColumns(Table.SelectRows(Table.Group(tbl, {"Custom"}, {{"Count", each Table.RowCount(_), type number}},GroupKind.Local), each [Custom]=1),{{"Count", each _ + 1}}) }}),
    Index = Table.AddIndexColumn(Group, "Index", 0, 1),
    Expand = Table.ExpandTableColumn(Index, "DATA", {"Custom", "Count"}, {"Custom", "Count"}),
    GroupCount = Table.Group(Expand, {"Count", "Index"}, {{"Count.1", each Table.RowCount(_), type number}}),
    Pivot = Table.Pivot(Table.TransformColumnTypes(GroupCount, {{"Count", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(GroupCount, {{"Count", type text}}, "en-US")[Count]), "Count", "Count.1", List.Sum),
    RemoveIndex = Table.RemoveColumns(Pivot,{"Index"}),
    OrderColumns = Table.ReorderColumns(RemoveIndex,List.Sort(Table.ColumnNames(RemoveIndex))),
    Total = Table.AddColumn(OrderColumns, "Total", each List.Sum(Record.FieldValues(_)))
in
    Total
 
Upvote 0
And, just for fun, VBA too.

MXL20201209 Sequence.xlsm
ABCDEFGHI
1DATA2 Consecutives3 Consecutives4 Consecutives5 consecutives6 consecutives7 consecutives8 consecutivesTotal
21,2,6,8,9,14,1822
31,2,3,411
45,10,11,16,18,2011
53,4,5,11,12,21,23112
610,13,14,15,16,1711
71,2,3,4,5,6,8,10,1211
81,3,5,6,7,8,9,10,1111
91,2,3,5,6,7,8,9,10,11,12112
Sheet5


VBA Code:
Sub SEQ()
Dim r As Range:         Set r = Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
Dim DATA() As Variant:  DATA = r.Value2
Dim RES() As Variant:   ReDim RES(1 To UBound(DATA))
Dim CNT As Integer:     CNT = 0
Dim SP() As String
Dim AR(1 To 7)

For d = LBound(DATA) To UBound(DATA)
    SP = Split(DATA(d, 1), ",")
    For i = LBound(SP) + 1 To UBound(SP)
        If SP(i) - SP(i - 1) = 1 Then
            CNT = CNT + 1
        Else
            If CNT > 0 Then
                AR(CNT) = AR(CNT) + 1
                CNT = 0
            End If
        End If
        If i = UBound(SP) And CNT > 0 Then AR(CNT) = AR(CNT) + 1
    Next i
    RES(d) = AR
    Erase AR
    CNT = 0
Next d

Set r = Range("B2").Resize(UBound(RES), UBound(AR))
r.Value2 = Application.Transpose(Application.Transpose(RES))
Set r = r.Resize(r.Rows.Count, 1).Offset(, 7)

With r
    .FormulaR1C1 = "=SUM(RC[-7]:RC[-1])"
    .Value = .Value2
End With

End Sub
 
Upvote 0
Not sure why but the limit was set to 14 instead of 20
9-12-20.xlsx
ABCDEFGHI
1DATA2 Consecutives3 Consecutives4 Consecutives5 consecutives6 consecutives7 consecutives8 consecutivesAll
21,2,6,8,9,14,182      2
31,2,3,4  1    1
45,10,11,16,18,201      1
53,4,5,11,12,21,2311     2
610,13,14,15,16,17   1   1
Sheet1
Cell Formulas
RangeFormula
B2:H6B2=IFERROR(1/(1/SUM(--(FREQUENCY(IF(ISNUMBER(SEARCH(","&ROW(B$1:B$20)&",",","&$A2&",")),ROW(B$1:B$20)),IF(ISERROR(SEARCH(","&ROW(B$1:B$20)&",",","&$A2&",")),ROW(B$1:B$20)))=COLUMN(B2)))),"")
I2:I6I2=IFERROR(1/(1/SUM(--(FREQUENCY(IF(ISNUMBER(SEARCH(","&ROW(I$1:I$20)&",",","&$A2&",")),ROW(I$1:I$20)),IF(ISERROR(SEARCH(","&ROW(I$1:I$20)&",",","&$A2&",")),ROW(I$1:I$20)))>1))),"")
Press CTRL+SHIFT+ENTER to enter array formulas.
If you don't mind my asking, why this formula doesn't give results? I couldn't figure it out. everything is same, except the cell locations.
ConsecutiveCount2.xlsm
BIBJBKBLBMBNBOBPBQ
1DATA2 Consecutives3 Consecutives4 Consecutives5 consecutives6 consecutives7 consecutives8 consecutivesAll
21,2,6,8,9,14,18        
31,2,3,4        
45,10,11,16,18,20        
53,4,5,11,12,21,23        
610,13,14,15,16,17        
Sheet1
Cell Formulas
RangeFormula
BJ2:BQ6BJ2=IFERROR(1/(1/SUM(--(FREQUENCY(IF(ISNUMBER(SEARCH(","&ROW(BJ$1:BJ$20)&",",","&$BI2&",")),ROW(BJ$1:BJ$20)),IF(ISERROR(SEARCH(","&ROW(BJ$1:BJ$20)&",",","&$BI2&",")),ROW(BJ$1:BJ$20)))=COLUMN(BJ2)))),"")
Press CTRL+SHIFT+ENTER to enter array formulas.
 
Upvote 0
COLUMN(BJ2) at the end of the formula is looking for a list of 62 consecutive numbers, not 2. Changing the method used there will resolve the problem.

Excel Formula:
=IFERROR(1/(1/SUM(--(FREQUENCY(IF(ISNUMBER(SEARCH(","&ROW(BJ$1:BJ$20)&",",","&$BI2&",")),ROW(BJ$1:BJ$20)),IF(ISERROR(SEARCH(","&ROW(BJ$1:BJ$20)&",",","&$BI2&",")),ROW(BJ$1:BJ$20)))=COLUMNS($BI2:BJ2)))),"")
 
Upvote 0
COLUMN(BJ2) at the end of the formula is looking for a list of 62 consecutive numbers, not 2. Changing the method used there will resolve the problem.

Excel Formula:
=IFERROR(1/(1/SUM(--(FREQUENCY(IF(ISNUMBER(SEARCH(","&ROW(BJ$1:BJ$20)&",",","&$BI2&",")),ROW(BJ$1:BJ$20)),IF(ISERROR(SEARCH(","&ROW(BJ$1:BJ$20)&",",","&$BI2&",")),ROW(BJ$1:BJ$20)))=COLUMNS($BI2:BJ2)))),"")
Thanks again, Jason. That solved the problem.
 
Upvote 0

Forum statistics

Threads
1,214,952
Messages
6,122,457
Members
449,083
Latest member
Ava19

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