Number Ranges/Series

duffy01

New Member
Joined
Jan 27, 2010
Messages
5
Hi All,

I'm trying to get a set of data into a certain format, automatically:

In Column A I have the following type of data -

3
4
5
6
9
11
14
15
16
17
33
37

1) I want to return the consecutive number series with less than 3 numbers like: 1,2

2) I also want to return the consecutive number series with 3 or more numbers like: 14-17

3) Then, I want the output of the data to be separated by commas.

4) That result would then be put in parentheses.

5) A count of the numbers would then appear before the parentheses.

The end result of the above example data would look like:

12 (3-6,9,11,14-17,33,37)

Can anyone help?

Thanks in advance!

Duff
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Hi, This could possibly be a lot simpler , but it seems to work.

Code:
[COLOR="Navy"]Sub[/COLOR] MG28Jan37
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, txt [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String,[/COLOR] c
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
c = c + 1
[COLOR="Navy"]If[/COLOR] c = 1 And Dn.Offset(1) = Dn + 1 [COLOR="Navy"]Then[/COLOR] txt = txt & Dn
[COLOR="Navy"]If[/COLOR] c = 1 And Not Dn.Offset(1) = Dn + 1 [COLOR="Navy"]Then[/COLOR] txt = txt & Dn & ","
[COLOR="Navy"]If[/COLOR] Not c = 1 [COLOR="Navy"]Then[/COLOR]
    [COLOR="Navy"]If[/COLOR] Not Dn.Offset(1) = Dn + 1 And Dn.Offset(-1) + 1 = Dn [COLOR="Navy"]Then[/COLOR]
        txt = txt & "-" & Dn & ","
    [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]If[/COLOR] Dn.Offset(1) = Dn + 1 And Not Dn.Offset(-1) + 1 = Dn [COLOR="Navy"]Then[/COLOR]
        txt = txt & Dn
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]If[/COLOR] Not Dn.Offset(-1) + 1 = Dn And Not Dn.Offset(1) = Dn + 1 [COLOR="Navy"]Then[/COLOR]
        txt = txt & Dn & ","
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] Dn
MsgBox Rng.Count & " (" & txt & ")"
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Mick,

This works fantastic for the example I gave.

How do I get it to work on a bigger range of numbers?
(i.e. cell A1 to A400)?

Thx

Duff
 
Upvote 0
Hi, The code should work for as many rows as you have filled with numbers in column "A", But you will end up with a rather large Msgbox.
What would you like to do with the result, put them in a range somewhere ???
Regards Mick
 
Upvote 0
Yes.. I need to return that msgbox data into a cell (the last number can't have a comma after it though)

Thanks... you rule!!

Duff

*Preferably cell B1.. thx!!
 
Last edited:
Upvote 0
Hi, Remove the magbox bit & replace with the code below.
Code:
txt = Left(txt, Len(txt) - 1)
[COLOR="Navy"]With[/COLOR] Range("b1")
    .Value = Rng.Count & " (" & txt & ")"
    .WrapText = True '[COLOR="Green"][B] remove this if not wanted[/B][/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
Regards Mick
 
Upvote 0
Mick,

This works 99.8% (which is a lot more than I had!)

If I have two consecutive numbers only, it puts a dash there.

I need anything less than 2 to be commas, and anything 3 or more to be the dash.

Thanks for all your help so far... this is incredible!!

Duff
 
Upvote 0
Hi, Duff, Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG29Jan17
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, txt [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String,[/COLOR] c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer,[/COLOR] n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer,[/COLOR] com
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
c = c + 1
[COLOR="Navy"]If[/COLOR] c = 1 And Dn.Offset(1) = Dn + 1 [COLOR="Navy"]Then[/COLOR] txt = txt & Dn
[COLOR="Navy"]If[/COLOR] c = 1 And Not Dn.Offset(1) = Dn + 1 [COLOR="Navy"]Then[/COLOR] txt = txt & Dn & ","
[COLOR="Navy"]If[/COLOR] Not c = 1 [COLOR="Navy"]Then[/COLOR]
    [COLOR="Navy"]If[/COLOR] Not Dn.Offset(1) = Dn + 1 And Dn.Offset(-1) + 1 = Dn [COLOR="Navy"]Then[/COLOR]
        txt = txt & "-" & Dn & ","
    [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]If[/COLOR] Dn.Offset(1) = Dn + 1 And Not Dn.Offset(-1) + 1 = Dn [COLOR="Navy"]Then[/COLOR]
        txt = txt & Dn
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]If[/COLOR] Not Dn.Offset(-1) + 1 = Dn And Not Dn.Offset(1) = Dn + 1 [COLOR="Navy"]Then[/COLOR]
        txt = txt & Dn & ","
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] Dn
txt = Left(txt, Len(txt) - 1)
    com = Split(txt, ",")
[COLOR="Navy"]For[/COLOR] n = 0 To UBound(com)
    [COLOR="Navy"]If[/COLOR] InStr(com(n), "-") > 0 [COLOR="Navy"]Then[/COLOR]
        [COLOR="Navy"]If[/COLOR] Val(Split(com(n), "-")(0) + 1) = Val(Split(com(n), "-")(1)) [COLOR="Navy"]Then[/COLOR]
            com(n) = Replace(com(n), "-", ",")
        [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] n
[COLOR="Navy"]With[/COLOR] Range("b1")
    .Value = Rng.Count & " (" & Join(com, ",") & ")"
    .WrapText = True '[COLOR="Green"][B] remove this if not wanted[/B][/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0

Forum statistics

Threads
1,214,849
Messages
6,121,925
Members
449,056
Latest member
denissimo

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