Expand Range of Values to Individual Values

RWeaverAMT

New Member
Joined
Aug 7, 2023
Messages
4
Office Version
  1. 2021
Platform
  1. Windows
I have a sheet which contains hyphenated number ranges. For example:

201-203
290-291
301-304

I want to "explode" these ranges into their constituent numbers (in individual cells), like so:

201
202
203
290
291
301
302
303
304

I've found some other threads with solutions posted, but am having issues getting them to work. For example, the following thread gives a VBA solution:


The VBA solution:

[table="width: 500"]
[tr]
[td]Function Mystr(S As String) As String
Dim X As Long, Z As Long, Start As Long, Finish As Long, Txt As String, Parts() As String
Parts = Split(S)
For X = 0 To UBound(Parts)
Txt = Left(Parts(X), Len(Parts(X)) / 2 - 3)
Start = Right(Left(Parts(X), Len(Parts(X)) / 2), 3)
Finish = Right(Parts(X), 3)
For Z = Start To Finish
Mystr = Mystr & " " & Txt & Z & Txt & Z
Next
Next
Mystr = Trim(Mystr)
End Function[/td]
[/tr]
[/table]

But when I put the code into VBA, then call the function in a cell, I get the following error:

1691442897166.png


It's strange because I'm copying the code exactly from the thread, and there are responses in the thread saying that the code worked for them. I can only assume that some Excel/VBA version change is at fault, as the solution is a few years old. I've found and tried other, similar solutions, with the same error as a result.

I don't know much VBA at all. Any input is much appreciated. Thank you.
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Your VBA code seems to have carried over some html table tags. Without delving into the code, i would say, make sure your code looks like the below image

The code's line must be "Function Mystr...." and it should end with "End Function" (nothing after this)
VBA Code Image.jpg
 
Upvote 0
Here is a way using Power Query.

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Transform = Table.TransformColumns(Source,{{"Ranges",each 
  let 
    x = List.Transform(Text.Split(_,"-"), Number.From),
    l = {x{0}..x{1}}
  in
    l
}}),
    Expand = Table.ExpandListColumn(Transform, "Ranges")
in
    Expand

Expand
ABC
1RangesRanges
2201-203201
3290-291202
4301-304203
5290
6291
7301
8302
9303
10304
Sheet7
 
Upvote 0
Here is a way using Power Query.

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Transform = Table.TransformColumns(Source,{{"Ranges",each
  let
    x = List.Transform(Text.Split(_,"-"), Number.From),
    l = {x{0}..x{1}}
  in
    l
}}),
    Expand = Table.ExpandListColumn(Transform, "Ranges")
in
    Expand

Expand
ABC
1RangesRanges
2201-203201
3290-291202
4301-304203
5290
6291
7301
8302
9303
10304
Sheet7

Thank you very much. I've got this working with the criteria I gave in my OP.

Now, what if I have a slightly different set of data, such as a column that looks like:

101
201-203
291-291
301-304

Your M Code above gives an error for the cell containing "101":

"There weren't enough elements in the enumeration to complete the operation."

Because the "101" cell doesn't contain any hyphenation, I'm guessing. Is there a way to address this?

Thanks again for your time.
 
Upvote 0
Your VBA code seems to have carried over some html table tags. Without delving into the code, i would say, make sure your code looks like the below image

The code's line must be "Function Mystr...." and it should end with "End Function" (nothing after this)
View attachment 96718

You are absolutely right. I can't believe I didn't notice the extra tags there. Thank you!
 
Upvote 0
This should take care of that issue.

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Transform = Table.TransformColumns(Source,{{"Ranges",each try
      let 
         x = List.Transform(Text.Split(_,"-"), Number.From),
         l = {x{0}..x{1}}
      in
         l
    otherwise {_}}}),
    Expand = Table.ExpandListColumn(Transform, "Ranges")
in
    Expand
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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