Complex Formula / VBA UDF Perhaps?

CROY1985

Well-known Member
Joined
Sep 21, 2009
Messages
501
Office Version
  1. 365
Platform
  1. Windows
Hi all

I would like to convert a string which would contain a mix of numbers and ranges (of numbers) into a complete list of integers.

The format would for example be:
1,3-4,6

and I want to convert this to a text string which would be:
1,3,4,6

A second example would be:
4,8-14,15,17-20

which i'd want to convert to:
4,8,9,10,11,12,13,14,15,17,18,19,20

Any pointers much appreciated.

Thanks
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Try this
VBA Code:
Sub Test_UDF()
    Debug.Print PopulateNumbers("4,8-14,15,17-20")
End Sub

Function PopulateNumbers(s As String)
    Dim x, y, out As String, i As Long, j As Long, m As Long, n As Long
    
    x = Split(s, ",")
    
    For i = LBound(x) To UBound(x)
        If InStr(x(i), "-") Then
            y = Split(x(i), "-")
            m = Application.Min(y(0), y(1))
            n = Application.Max(y(0), y(1))
            
            For j = m To n
                out = out & IIf(out <> "", ",", "") & j
            Next j
        Else
            out = out & IIf(out <> "", ",", "") & x(i)
        End If
    Next i
    
    PopulateNumbers = out
End Function
 
Upvote 0
Here is my UDF
VBA Code:
Function ListNums(s As String) As String
  Dim sp1 As Variant, sp2 As Variant
  Dim i As Long
 
  For Each sp1 In Split(s, ",")
    sp2 = Split(sp1, "-")
    For i = sp2(0) To sp2(UBound(sp2))
      ListNums = ListNums & "," & i
    Next i
  Next sp1
  ListNums = Mid(ListNums, 2)
End Function

Book1
AB
11,3-4,61,3,4,6
24,8-14,15,17-204,8,9,10,11,12,13,14,15,17,18,19,20
Sheet1
Cell Formulas
RangeFormula
B1:B2B1=ListNums(A1)
 
Upvote 0
This looked like a fun one to try. Here's my version for kicks.

Code:
Function SEQUENCE(s As String)
Dim SP() As String: SP = Split(s, ",")
Dim SP2() As String
Dim AR As Variant

For i = 0 To UBound(SP)
    If InStr(SP(i), "-") > 0 Then
        SP2 = Split(SP(i), "-")
        AR = Evaluate("Transpose(Row(" & SP2(0) & ":" & SP2(1) & "))")
        SP(i) = Join(SP2, ",")
    End If
Next i

SEQUENCE = Join(SP, ",")
End Function
 
Upvote 0
Here is another function that you can try...
VBA Code:
Function Sequence(ByVal S As String) As String
  Dim X As Long, V As Variant
  For Each V In Split(Replace(S, "-", ":"), ",")
    For X = Evaluate("MIN(" & Replace(S, "-", ",") & ")") To Evaluate("MAX(" & Replace(S, "-", ",") & ")")
      If Not Intersect(Rows(V), Rows(X)) Is Nothing Then Sequence = Sequence & "," & X
    Next
  Next
  Sequence = Mid(Sequence, 2)
End Function
 
Upvote 0

Forum statistics

Threads
1,215,026
Messages
6,122,738
Members
449,094
Latest member
dsharae57

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