How to expand data

ouadad

Board Regular
Joined
Jun 19, 2005
Messages
213
I have data that looks like this:
# of parts cost per part
100 36.50
200 25.00
10 34.95
.
.
.
68,000 values

and I need to expand the list so that it's a list of, for example, 100 rows of 36.50, 200 rows of 25.00, 10 rows of 34.95,etc.. The goal is to calculate a cumulative cost of all parts.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
put this code into a module ,
then run ExapandVals

Code:
Sub ExapandVals()
Dim shSrc As Worksheet, shTarg As Worksheet
Dim iQty As Integer
Dim nTot As Currency
Dim vCost
Set shSrc = ActiveSheet
Sheets.Add
Set shTarg = ActiveSheet
shSrc.Activate
Range("A2").Select
While ActiveCell.Value <> ""
iQty = ActiveCell.Value
vCost = ActiveCell.Offset(0, 1).Value

For i = 1 To iQty
shTarg.Activate
ActiveCell.Value = vCost
nTot = nTot + vCost
ActiveCell.Offset(0, 1).Value = nTot

ActiveCell.Offset(1, 0).Select 'next row
Next

shSrc.Activate
ActiveCell.Offset(1, 0).Select 'next row
nTot = 0
Wend
End Sub
 
Upvote 0
Is it absolutely necessary for you to expand the list?

Does something like this help?
Book1
ABC
1QtyCost
2100 36.50100 3,650.00
3200 25.00200 5,000.00
410 34.9510 349.50
Sheet1
Cell Formulas
RangeFormula
B2:B4B2=TRIM(LEFT(SUBSTITUTE(TRIM(A2)," ",REPT(" ",20),1),10))
C2:C4C2=IFERROR(B2*TRIM(RIGHT(SUBSTITUTE(TRIM(A2)," ",REPT(" ",20),1),10)),"")
 
Upvote 0
Thanks Ranman256. Yes, Snakehips it's necessary to generate a cumulative cost for all parts rather than bunches of parts.
 
Upvote 0
This is another macro that does the same thing but should be significantly faster

VBA Code:
Sub AddLines()

Dim a, j&: j = 1
a = [A1].CurrentRegion

ReDim b(1 To [sum(A:A)], 1 To 2)
For x = 1 To UBound(a)
    For i = j To a(x, 1) + j - 1
        b(i, 1) = a(x, 2)
        b(i, 2) = IIf(j = 1, a(x, 2) * i, a(x, 2) * (i - j + 1))
    Next
    j = j + a(x, 1)
Next

Sheets.Add(, ActiveSheet).[A1].Resize(UBound(b), 2) = b

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,815
Messages
6,181,136
Members
453,021
Latest member
Justyna P

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