Converting a mixed list of numbers and number ranges into a single list in a new column

That_Guy86

New Member
Joined
Oct 24, 2023
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hello-

I'm attempting to take a list of numbers that can be single numbers (123456) or a range separated by a hyphen (123457-123500) contained in different cells within 1 column and have it produce a list of every number in another column.

For example:

123456
123457
123458
123459
123460
..
123498
123499
123500

I'm attempting to automate this process as much as possible.

Thanks in advance.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Welcome to MrExcel. Perhaps something like this.
VBA Code:
Sub ExpandList()
    Dim WS As Worksheet
    Dim rng As Range
    Dim R As Range
    Dim I As Long
    Dim Ofs As Long
    Dim VA As Variant
    
    Set WS = ActiveSheet

    With WS
        Set rng = .Range("A2", .Range("A" & .Rows.Count).End(xlUp))
        .Range("B2", .Range("B" & .Rows.Count).End(xlUp)).ClearContents
    End With

    Ofs = 0

    For Each R In rng
        VA = Split(R.Value, "-")
        Select Case UBound(VA)
        Case 0
            rng.Range("A1").Offset(Ofs, 1).Value = VA(0)
            Ofs = Ofs + 1
        Case 1
            For I = VA(0) To VA(1)
                rng.Range("A1").Offset(Ofs, 1).Value = I
                Ofs = Ofs + 1
            Next I
        Case Else
        End Select
    Next R
End Sub
Book2
AB
1ListExpanded List
2123456123456
3123457-123470123457
4800123458
5995123459
6996-1007123460
7123461
8123462
9123463
10123464
11123465
12123466
13123467
14123468
15123469
16123470
17800
18995
19996
20997
21998
22999
231000
241001
251002
261003
271004
281005
291006
301007
Sheet6
 
Upvote 0
Welcome to the MrExcel board!

Especially if the final list might be a long one, this should be quicker. I've assumed data in A2 down and results in column B.
Test with a copy of your workbook.

VBA Code:
Sub ExpandList_v2()
  Dim a As Variant, b As Variant, Bits As Variant
  Dim i As Long, j As Long, k As Long
  
  a = Range("A2", Range("A" & Rows.Count).End(xlUp)).Value
  ReDim b(1 To Rows.Count, 1 To 1)
  For i = 1 To UBound(a)
    Bits = Split(a(i, 1) & "-" & a(i, 1), "-")
    For j = Bits(0) To Bits(1)
      k = k + 1
      b(k, 1) = j
    Next j
  Next i
  Range("B2").Resize(k).Value = b
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,691
Members
449,117
Latest member
Aaagu

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