RWeaverAMT
New Member
- Joined
- Aug 7, 2023
- Messages
- 4
- Office Version
- 2021
- Platform
- 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:
But when I put the code into VBA, then call the function in a cell, I get the following error:
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.
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:
Expand range of numbers
I have a situation that I need help with. In cell “A2” I have the following “789-793”. I would like to expand the sequence of numbers anytime a “-“ is present by using the 789 as the start (in this example) of the sequence and then using 793 as the end (in this example) of the sequence. I would...
www.mrexcel.com
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:
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.