transpose and expand

Danny54

Active Member
Joined
Jul 3, 2019
Messages
295
Office Version
  1. 365
Platform
  1. Windows
How would I go about splitting by the comma, transpose to column b, then expand the values between the dash?

Input sheet
1634130626597.png


New Sheet
1634130738362.png


Thanks
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Try this:
VBA Code:
Sub MyMacro()

    Dim str1 As String
    Dim arr1() As String
    Dim arr2() As String
    Dim i As Long
    Dim r As Long
    Dim mn As Long
    Dim mx As Long
    Dim j As Long
    
    Application.ScreenUpdating = False
    
'   Designate range of values
    str1 = Range("A1").Value
    
'   Create array of values, separated by comma
    arr1 = Split(str1, ",")
    
'   Loop through each value in array
    For i = LBound(arr1) To UBound(arr1)
'       Check to see if there is a range in the value
        If InStr(1, arr1(i), "-") > 0 Then
'           Split values
            arr2 = Split(arr1(i), "-")
'           Populate values in column B
            mn = arr2(0)
            mx = arr2(1)
            For j = mn To mx
                r = r + 1
                Cells(r, "B") = j
            Next j
        Else
'           Populate single value in column B
            r = r + 1
            Cells(r, "B") = arr1(i)
        End If
    Next i
          
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Solution
You are welcome.
Glad I was able to help.
 
Upvote 0
An alternative UDF version:

VBA Code:
Function EXPANDANDTRANSPOSE(rng As Range)
Dim arrCommaSep As Variant
Dim itemVal1 As Variant
Dim arrDashSep As Variant
Dim itemVal2 As Variant
Dim i As Integer
Dim tmpCollection As New Collection
Dim tmpArray() As Integer

    If rng.Cells.Count > 1 Then
        EXPANDANDTRANSPOSE = CVErr(xlErrValue)
    Else
        arrCommaSep = Split(rng, ",")
        For Each itemVal1 In arrCommaSep
            arrDashSep = Split(itemVal1, "-")
            tmpCollection.Add arrDashSep(0)
            If UBound(arrDashSep) Then
                For i = 1 To arrDashSep(UBound(arrDashSep)) - arrDashSep(0)
                    tmpCollection.Add arrDashSep(0) + i
                Next i
            End If
        Next itemVal1
    
        ReDim tmpArray(0, 1 To tmpCollection.Count)
        For i = 1 To tmpCollection.Count
            tmpArray(0, i) = tmpCollection.Item(i)
        Next i
    
        EXPANDANDTRANSPOSE = Application.WorksheetFunction.Transpose(tmpArray)
    End If
End Function
Book1
AB
11-3, 5, 7, 9,12-14,17,350-3541
22
33
45
57
69
712
813
914
1017
11350
12351
13352
14353
15354
Sheet1
Cell Formulas
RangeFormula
B1:B15B1=EXPANDANDTRANSPOSE(A1)
Dynamic array formulas.
 
Upvote 0
I made a VBA free solution. But I couldn't boil it all down to one formula, because the formula got too long for excel to handle. I'll bet the LET function could make quick work of it - I'd love to see that:

MrExcelPlayground4.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAAB
1InputOutputFirst , or -Last , or -first numberlast numbertotal sequencesplit by commanumber of groups (+1 needed)Unfiltered solutionmin for dash groupsmax for dash groupsfiltered dash minfiltered dash maxcount of dash groupswidest dash grouparray of dash groupvector of dash group
21-3,5,7,9,12-14,17,350-3521223135211-3611313331231
32252#VALUE!#VALUE!12141213142
43373#VALUE!#VALUE!3503523503513523
55490#VALUE!#VALUE!12
67512-145121413
796170#VALUE!#VALUE!14
8127350-3527350352350
91380351
101499352
1117100
12350110
133511212
143521313
151414
16150
17160
181717
19180
20190
21200
22210
23220
24230
25240
26250
27260
28270
29280
30290
31300
32310
33320
34330
35340
36350
37360
38370
Sheet18
Cell Formulas
RangeFormula
B2:B14B2=FILTER(L2#,L2#>0)
D2D2=MIN(SEARCH("-",A2),SEARCH(",",A2))
E2E2=MAX(IFERROR(SEARCH("-",A2,SEQUENCE(LEN(A2))),0),IFERROR(SEARCH(",",A2,SEQUENCE(LEN(A2))),0))
F2F2=VALUE(LEFT(A2,D2-1))
G2G2=VALUE(RIGHT(A2,LEN(A2)-E2))
H2:H353H2=SEQUENCE(G2-F2+1,1,F2,1)
I2:I8I2=TRIM(MID(SUBSTITUTE($A2,",",REPT(" ",999)),SEQUENCE(J2+1)*999-998,999))
J2J2=LEN(A2)-LEN(SUBSTITUTE(A2,",",""))
L2:L353L2=IF(NOT(ISERROR(MATCH(H2#,--I2#,0))),H2#,(--NOT(ISNA(MATCH(H2#,AB2#,0))))*H2#)
O2:O8O2=VALUE(LEFT(I2#,SEARCH("-",I2#)-1))
P2:P8P2=VALUE(RIGHT(I2#,LEN(I2#)-SEARCH("-",I2#)))
R2:S4R2=FILTER(O2#,ISNUMBER(VALUE(O2#)))
U2U2=ROWS(R2#)
V2V2=MAX(S2#-R2#)+1
X2:Z4X2=SEQUENCE(U2,V2,0,0)+R2#+SEQUENCE(1,V2,0,1)*(--(SEQUENCE(U2,V2,0,0)+R2#+SEQUENCE(1,V2,0,1)<=S2#))
AB2:AB10AB2=SEQUENCE(ROWS(X2#)*COLUMNS(X2#),1,1,0)*INDEX(X2#,INT((SEQUENCE(ROWS(X2#)*COLUMNS(X2#))-1)/COLUMNS(X2#))+1,MOD(SEQUENCE(ROWS(X2#)*COLUMNS(X2#))-1,COLUMNS(X2#))+1)
Dynamic array formulas.


Without the LET, the array in X will spill if the biggest 'dash' group is more than 3.
 
Upvote 0

Forum statistics

Threads
1,223,934
Messages
6,175,490
Members
452,649
Latest member
mr_bhavesh

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