This is from another answer I received a while back.
Turn a value range in a cell into a comma separated list
This code below is great stuff, but now I want it to also output each number with 4 digits. So if I get a return of 1,2,3, I want it to return as "0001,0002,0003".
Function NumRange(v)
Dim arrC, arr, x As Long, rv As String, sep As String, e
arrC = Split(v, ",")
rv = ""
For Each e In arrC
If InStr(e, "-") Then
arr = Split(e, "-")
arr(0) = Trim(arr(0))
arr(1) = Trim(arr(1))
If IsNumeric(arr(0)) And IsNumeric(arr(1)) Then
For x = CLng(arr(0)) To CLng(arr(1))
rv = rv & sep & x
sep = ","
Next x
End If
ElseIf IsNumeric(e) Then
rv = rv & sep & CLng(e)
sep = ","
End If
Next e
NumRange = rv
End Function
It's a little brute-force, but you could use a variant of this handy translation of the LPAD
or PadLeft()
function/method:
Any method equivalent to PadLeft/PadRight?
Function NumRange(v)
Dim arrC, arr, x As Long, rv As String, sep As String, e
arrC = Split(v, ",")
rv = ""
For Each e In arrC
If InStr(e, "-") Then
arr = Split(e, "-")
arr(0) = Trim(arr(0))
arr(1) = Trim(arr(1))
If IsNumeric(arr(0)) And IsNumeric(arr(1)) Then
For x = CLng(arr(0)) To CLng(arr(1))
rv = rv & sep & Right("0000" & x, 4)
sep = ","
Next x
End If
ElseIf IsNumeric(e) Then
rv = rv & sep & Right("0000" & e, 4)
sep = ","
End If
Next e
NumRange = rv
End Function
Now a cell that contains the formula:
=NumRange("1-10")
Will contain:
001,002,003,004,005,006,007,008,009,010