This can be done with formulas, if all of your A1 entries are exactly as you
describe. However, it is more easily done with a UDF, which can also be
adapted in case your A1 content does not consist solely of separated integers.
The function below will return all the integer values as NUMERIC data, or a
blank.
It is used in the form of =ExtrNums(cell_ref, Index)
To enter this User Defined Function (UDF), <alt-F11> opens the Visual Basic
Editor.
Ensure your project is highlighted in the Project Explorer window.
Then, from the top menu, select Insert/Module and
paste the code below into the window that opens.
To use this User Defined Function (UDF):
B1: =ExtrNums($A$1,ROWS($1:1))
Then fill down until you begin returning blanks.
If it does not precisely meet your requirements, it can be easily modified.
=============================
Option Explicit
Function ExtrNums(s As String, Index As Long) As Variant
Dim re As Object, mc As Object
Const sPat As String = "\d+"
Set re = CreateObject("vbscript.regexp")
re.Pattern = sPat
re.Global = True
Set mc = re.Execute(s)
If Index > mc.Count Then
ExtrNums = ""
Else
ExtrNums = CDbl(mc(Index - 1))
End If
Set re = Nothing
End Function
====================================
--ron
I see from Mike's post I misinterpreted your request, and missed the fact that
you wanted all of the values returned if the integers were separated by a
hyphen.
Mike's solution should work fine.
If you would rather use a UDF that returns an array, instead of a Sub, you
could use the UDF below.
You could use it in a variety of ways
B1: =INDEX(ExtrNums($A$1),ROWS($1:1))
and fill down until you get errors
B1:
=IF(ISERR(INDEX(ExtrNums($A$1),ROWS($1:1))),"",INDEX(ExtrNums($A$1),ROWS($1:1)))
and fill down until you get blanks.
================================
Option Explicit
Function ExtrNums(s As String) As Variant
Dim aRes1, aRes2()
Dim aTemp As Variant
Dim sTemp As String
Dim i As Long, j As Long, k As Double, l As Double, m As Double
sTemp = Replace(s, ";", ",")
aRes1 = Split(sTemp, ",")
j = 0
ReDim aRes2(0 To UBound(aRes1))
For i = 0 To UBound(aRes1)
If IsNumeric(aRes1(i)) Then
aRes2(j) = CDbl(aRes1(i))
j = j + 1
Else
l = Val(aRes1(i)): m = Mid(aRes1(i), InStr(aRes1(i), "-") + 1)
ReDim Preserve aRes2(UBound(aRes2) + m - l)
For k = l To m
aRes2(j) = k
j = j + 1
Next k
End If
Next i
ExtrNums = aRes2
End Function
==================================
--ron