Explode a list based on a quantity

cwolfe8229

New Member
Joined
May 29, 2007
Messages
29
I need some help from the experts please. I have a list of materials, which varies in length by day. I need to explode this list into individual line items that repeat based on Qty, kind of like a reverse Pivot Table. This list is typically hundreds of line items per day. For example:

MaterialQty
ABC12
XYZ13

<tbody>
</tbody>

needs to become:

MaterialQty
ABC11
ABC11
XYZ11
XYZ11
XYZ11

<tbody>
</tbody>

Any suggestions?
Thanks
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Here is some VBA code that should do what you want:
Code:
Sub ExplodeList()

    Dim lr As Long
    Dim r As Long
    Dim ct As Long
    
    Application.ScreenUpdating = False
    
'   Find last row in column B with data
    lr = Cells(Rows.Count, "B").End(xlUp).Row
    
'   Loop through all rows going backwards
    For r = lr To 2 Step -1
'       Get count of rows needed
        ct = Cells(r, "B")
'       Insert needed rows
        If ct > 1 Then
            Rows(r + 1 & ":" & r + ct - 1).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
            Range(Cells(r + 1, "A"), Cells(r + ct - 1, "A")) = Cells(r, "A")
            Range(Cells(r, "B"), Cells(r + ct - 1, "B")) = 1
        End If
    Next r
    
    Application.ScreenUpdating = True
    
End Sub
Note that this code assumes:
- Data is in columns A and B
- Header row in row 1
- Data starts on row 2

Code may need to be amended slightly if those conditions don't match exactly.
 
Upvote 0
maybe

MaterialQtyMaterialQty
ABC1
2​
ABC1
2​
XYZ1
3​
ABC1
2​
XYZ1
3​
XYZ1
3​
XYZ1
3​

Code:
[SIZE=1]// Table1
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    List = Table.AddColumn(Source, "Custom", each {1..[Qty]}),
    Expand = Table.ExpandListColumn(List, "Custom"),
    RC = Table.RemoveColumns(Expand,{"Custom"})
in
    RC[/SIZE]
 
Last edited:
Upvote 0
Joe4, Thank you, that worked and I was able to edit the code to add columns of additional data.

sandy444, Thank you too, but I am a complete novice in VBA and macros and your solution looks cleaner, but I couldn't figure out how to get it to work.
 
Upvote 0
You are welcome.

Note that Sandy's response uses Power Query. Also, I think the Qty field would need some adjustment, as it is returning the original amount instead of the 1s you wanted on each line,
 
Upvote 0
sandy444, Thank you too, but I am a complete novice in VBA and macros and your solution looks cleaner, but I couldn't figure out how to get it to work.
sandy666 :LOL:

and this is Power Query M-code, not vba

edit:
Thanks Joe :)
 
Last edited:
Upvote 0
Just for completeness, here's a formula method:

ABCDE
1MaterialQtyMaterialQty
2ABC12ABC11
3XYZ13ABC11
4QQQ4XYZ11
5XYZ11
6XYZ11
7QQQ1
8QQQ1
9QQQ1
10QQQ1
11

<colgroup><col style="width: 25pxpx"><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet3

Worksheet Formulas
CellFormula
E2=IF(D2="","",1)

<thead>
</thead><tbody>
</tbody>

<tbody>
</tbody>

Array Formulas
CellFormula
D2{=INDEX($A$2:$A$5,MATCH(ROWS($D$2:$D2)-1,SUBTOTAL(9,OFFSET($B$1,0,0,ROW($B$2:$B$5)-ROW($B$2)+1))))&""}

<thead>
</thead><tbody>
</tbody>
Entered with Ctrl+Shift+Enter. If entered correctly, Excel will surround with curly braces {}.
Note: Do not try and enter the {} manually yourself

<tbody>
</tbody>



Although to be honest, I think either of the previous solutions would be better.
 
Upvote 0

Forum statistics

Threads
1,214,549
Messages
6,120,149
Members
448,948
Latest member
spamiki

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