Convert List Of Sequential Numbers Into Ranges

nmganey

New Member
Joined
Oct 11, 2016
Messages
18
hey i have a list in the same cell separated by ,
ex:
[TABLE="width: 229"]
<colgroup><col width="229"></colgroup><tbody>[TR]
[TD="width: 229"]1,2,3,4,5,9,22,23,24,25,244,245,246,247,248,249
[/TD]
[/TR]
</tbody>[/TABLE]
and so on i want it to be converted into 1-5,9,22-25,244-249 any help would be much appreciated.Thanks
 
Thanks so much I am pretty sure that has done the job.

I have used the code:

Function IntoRanges(aString As String, Optional Delimiter As String = ",") As String
Dim NextBit As String
Dim i As Long
Dim Items As Variant
Items = Split(aString, Delimiter)

IntoRanges = Items(0)
For i = 0 To UBound(Items) - 1
If Asc(Items(i)) + 1 = Asc(Items(i + 1)) Then
NextBit = "-" & Items(i + 1)
Else
If NextBit = vbNullString Then
IntoRanges = IntoRanges & Delimiter & Items(i + 1)


Else
IntoRanges = IntoRanges & NextBit & Delimiter & Items(i + 1)
NextBit = vbNullString
End If
End If
Next i
IntoRanges = IntoRanges & NextBit
End Function
 
Upvote 0

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