Total in group of 3

pksinghal

New Member
Joined
Apr 10, 2015
Messages
38
Hello Seniors,

I need urgent help in following excel sheet.

[TABLE="width: 500"]
<tbody>[TR]
[TD]Quantity[/TD]
[TD]Price[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]34.00[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]34.50[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]22.34[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]34.22[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]43.22[/TD]
[/TR]
</tbody>[/TABLE]

I want to get the data in new table as

[TABLE="width: 500"]
<tbody>[TR]
[TD]3[/TD]
[TD]"WEIGHTED AVG PRICE OF FIRST 3 QUANTITY:[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]"WEIGHTED AVG PRICE OF NEXT 3 QUANTITY.[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]"WEIGHTED AVG PRICE OF NEXT 3 QUANTITIY[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]SO....ON[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]



Please help and provide excel formula to do it.
 
In simple terms, we need to test:

=MAX(0,MIN(0,Qty,Size, Qty+PrevCumulative-PreviousUpperBoundary,Qty+CurrentUpperBoundary-Cumulative))

Correction:

=MAX(0,MIN(Qty, Size, Qty+PrevCumulative-PreviousUpperBoundary,Qty+CurrentUpperBoundary-Cumulative))
 
Upvote 0

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Please make it for "n" for quantity and "n" number of price.

That's why I used range names back in Post #2 . It will take you just a few seconds to change:

Qty =A2:A6
to
Qty=A2:A10, for example.

Or you could define each range name dynamically, e.g. Qty =A2:INDEX(A:A,COUNTA(A:A)).

If you are happy with a VBA solution, then I would do it directly rather than in the previous roundabout way using Excel vectors.

Using the layout in Post #9 , I4: =WeightedAverage(Qty,Prices,E4,Size)

Code:
Function WeightedAverage(Qty As Variant, Price As Variant, Start As Double, Size As Double) As Variant

    Dim dCumulativeQty As Double, dQtyRequired As Double, dQtyUsed As Double, dTemp As Double
    Dim vQty As Variant, vPrice As Variant
    Dim i As Long
    
    vQty = Qty
    vPrice = Price
    dQtyRequired = Size
    
    For i = 1 To UBound(vQty)
        dCumulativeQty = dCumulativeQty + vQty(i, 1)
        If dCumulativeQty >= Start Then
            dQtyUsed = Application.min(dQtyRequired, vQty(i, 1), dCumulativeQty + 1 - Start)
            dTemp = dTemp + vPrice(i, 1) * dQtyUsed
            dQtyRequired = dQtyRequired - dQtyUsed
            If dQtyRequired = 0 Then Exit For
        End If
    Next i
    
    WeightedAverage = IIf(dQtyRequired = 0, dTemp / Size, "-")

End Function
 
Upvote 0
Hi Stephen,


Thanks for great support..


Could you please finally attach your excel sheet which use range for "n" price and "n" quantity.

I dont want VBA solution..

Please help






That's why I used range names back in Post #2 . It will take you just a few seconds to change:

Qty =A2:A6
to
Qty=A2:A10, for example.

Or you could define each range name dynamically, e.g. Qty =A2:INDEX(A:A,COUNTA(A:A)).

If you are happy with a VBA solution, then I would do it directly rather than in the previous roundabout way using Excel vectors.

Using the layout in Post #9 , I4: =WeightedAverage(Qty,Prices,E4,Size)

Code:
Function WeightedAverage(Qty As Variant, Price As Variant, Start As Double, Size As Double) As Variant

    Dim dCumulativeQty As Double, dQtyRequired As Double, dQtyUsed As Double, dTemp As Double
    Dim vQty As Variant, vPrice As Variant
    Dim i As Long
    
    vQty = Qty
    vPrice = Price
    dQtyRequired = Size
    
    For i = 1 To UBound(vQty)
        dCumulativeQty = dCumulativeQty + vQty(i, 1)
        If dCumulativeQty >= Start Then
            dQtyUsed = Application.min(dQtyRequired, vQty(i, 1), dCumulativeQty + 1 - Start)
            dTemp = dTemp + vPrice(i, 1) * dQtyUsed
            dQtyRequired = dQtyRequired - dQtyUsed
            If dQtyRequired = 0 Then Exit For
        End If
    Next i
    
    WeightedAverage = IIf(dQtyRequired = 0, dTemp / Size, "-")

End Function
 
Upvote 0
Try this small modification in StephenCrump's suggestion:

1) Create the names below:

Qty - Refers to: =CalcQty!$A$2:$A$13

Prices - Refers to: =CalcQty!$B$2:$B$13

Size - Refers to: =CalcQty!$E$1

FromTo - Refers to: =CalcQty!$D:$D

Cumulative - Refers to: =MMULT(--((ROW(INDIRECT("1:"&ROWS(Qty)))>=TRANSPOSE(ROW(INDIRECT("1:"&ROWS(Qty)))))),Qty)

PrevCumulative - Refers to: =MMULT(--((ROW(INDIRECT("1:"&ROWS(Qty)))-1>=TRANSPOSE(ROW(INDIRECT("1:"&ROWS(Qty)))))),Qty)

CalcCumulative - Refers to: =Cumulative-(ROW()-ROW(CalcQty!$D$3))*Size

CalcPrevCumulative - Refers to: =(ROW()-ROW(CalcQty!$D$4)))*Size-PrevCumulative

2) Put the formula below in D4 and copy down:

=Size*(ROWS(D$4:D4)-1)+1&"-"&Size*ROWS(D$4:D4)

3) Put the Array Formula (use Ctrl+Shift+Enter to enter the formula) below in E4 and copy down:

=SUM(Prices*(Cumulative>(ROWS(E$4:E4)-1)*Size)*(PrevCumulative < ROWS(E$4:E4)*Size)*
(Qty-NOT((CalcCumulative<0)*(CalcPrevCumulative<0))*
IF(CalcCumulative>CalcPrevCumulative,CalcCumulative,CalcPrevCumulative)))/Size



[TABLE="class: grid, width: 413"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[TD]E[/TD]
[TD]F[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Quantity[/TD]
[TD]Price[/TD]
[TD][/TD]
[TD]Group Size[/TD]
[TD]3[/TD]
[TD]Sheet[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]2[/TD]
[TD]34[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]CalcQty[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]2[/TD]
[TD]34,5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[TD][/TD]
[/TR]
[TR]
[TD]15[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]34-36[/TD]
[TD]46,81[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]16[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]****[/TD]
[TD]********[/TD]
[TD]********[/TD]
[TD]***[/TD]
[TD]***********[/TD]
[TD]*********[/TD]
[TD]*********[/TD]
[/TR]
</tbody>[/TABLE]

Do some tests.

Markmzz
 
Upvote 0
Hi!

Try this small modification in my last suggestion (Post #16):

1) Create the names below:

Qty - Refers to: =CalcQty!$A$2:INDEX(CalcQty!$A:$A,MATCH(8^7,CalcQty!$A:$A,1))

Prices - Refers to: =CalcQty!$B$2:INDEX(CalcQty!$B:$B,MATCH(8^7,CalcQty!$A:$A,1))

FromTo - Refers to: =CalcQty!$D$4:INDEX(CalcQty!$D:$D,MATCH("ZZZ",CalcQty!$D:$D,1))

SizeN - Refers to: =ROW()-ROW(INDEX(FromTo,1))+1

SizeN_1 - Refers to: =ROW()-ROW(INDEX(FromTo,1))

CalcCumulative - Refers to: =Cumulative-SizeN

CalcPrevCumulative - Refers to: =SizeN_1-PrevCumulative

2) Put the Array Formula (use Ctrl+Shift+Enter to enter the formula) below in E4 and copy down:

=SUM(Prices*(Cumulative>SizeN_1)*(PrevCumulative<sizen)*
(Qty-(CalcCumulative>0)*CalcCumulative-(CalcPrevCumulative>0)*CalcPrevCumulative))/Size
</sizen)*

Markmzz
 
Upvote 0
Hi!

Try this small modification in my last suggestion (Post #16):

2) Put the Array Formula (use Ctrl+Shift+Enter to enter the formula) below in E4 and copy down:

=SUM(Prices*(Cumulative>SizeN_1)*(PrevCumulative<sizen)*
(Qty-(CalcCumulative>0)*CalcCumulative-(CalcPrevCumulative>0)*CalcPrevCumulative))/Size
</sizen)*

Markmzz

Try the Normal Formula below too:

=SUMPRODUCT(Prices,--(Cumulative>SizeN_1),--(PrevCumulative < SizeN),(Qty-(CalcCumulative>0)*CalcCumulative-(CalcPrevCumulative>0)*CalcPrevCumulative))/Size<sizen),
</sizen),
<sizen),


By the way, the correct Array Formula of my last post is:

=SUM(Prices*(Cumulative>SizeN_1)*(PrevCumulative < SizeN)*(Qty-(CalcCumulative>0)*CalcCumulative-(CalcPrevCumulative>0)*CalcPrevCumulative))/Size

Markmzz
</sizen),
 
Last edited:
Upvote 0
Could you please finally attach your excel sheet which use range for "n" price and "n" quantity.

Workbook: https://app.box.com/s/z9d0ge9nhr89wm59ji2n2li7f1rjj42l

G4: <f4)*if(size<if(qty<if(qty-(cumulative-f4)<qty-(f3-prevcumulative),qty-(cumulative-f4),qty-(f3-prevcumulative)),qty,if(qty-(cumulative-f4)<qty-(f3-prevcumulative),qty-(cumulative-f4),qty-(f3-prevcumulative))),size,if(qty<if(qty-(cumulative-f4)<qty-(f3-prevcumulative),qty-(cumulative-f4),qty-(f3-prevcumulative)),qty,if(qty-(cumulative-f4)<qty-(f3-prevcumulative),qty-(cumulative-f4),qty-(f3-prevcumulative))))) size,"-")
=IF(SUM(Qty)>=ROWS(G$4:G4)*Size,SUMPRODUCT(Prices,(Cumulative>F3)*(PrevCumulative < F4)*IF(Size < IF(Qty < IF(Qty-(Cumulative-F4) < Qty-(F3-PrevCumulative),Qty-(Cumulative-F4),Qty-(F3-PrevCumulative)),Qty,IF(Qty-(Cumulative-F4) < Qty-(F3-PrevCumulative),Qty-(Cumulative-F4),Qty-(F3-PrevCumulative))),Size,IF(Qty < IF(Qty-(Cumulative-F4) < Qty-(F3-PrevCumulative),Qty-(Cumulative-F4),Qty-(F3-PrevCumulative)),Qty,IF(Qty-(Cumulative-F4) < Qty-(F3-PrevCumulative),Qty-(Cumulative-F4),Qty-(F3-PrevCumulative)))))/Size,"-")

where:

Qty: ='1'!$A$2:INDEX('1'!$A:$A,COUNTA('1'!$A:$A))
Prices: ='1'!$B$2:INDEX('1'!$B:$B,COUNTA('1'!$A:$A))
Cumulative: ='1'!$C$3:INDEX('1'!$C:$C,1+COUNTA('1'!$A:$A))
PrevCumulative: ='1'!$C$2:INDEX('1'!$C:$C,COUNTA('1'!$A:$A))

These dynamic ranges rely on there being no content further down in column A.
'1' is just my worksheet name - adjust as appropriate.

Size: ='1'!$G$1

I have also used one of Markmzz's more elegant formulae:

H4: =IF(SUM(Qty)>=ROWS(G$4:G4)*Size,SUMPRODUCT(Prices,--(Cumulative>F3),--(PrevCumulative < F4),(Qty-(CalcCumulative>0)*CalcCumulative-(CalcPrevCumulative>0)*CalcPrevCumulative))/Size,"-")
<f4),(qty-(calccumulative>
where in addition to the above helper formula, we also need:

CalcCumulative: =Cumulative-(ROW()-ROW('1'!$H$3))*Size
CalcPrevCumulative: =(ROW()-ROW('1'!$H$4))*Size-PrevCumulative


Book1
ABCDEFGH
1QuantityPriceCumulativeGroup Size3
2234.000FromTo
3234.5020Option AOption B
4122.3441334.1734.17
5334.2254630.3530.35
6243.2287937.2237.22
7234.2210101237.2237.22
8623.4412131523.4423.44
9534.8818161823.4423.44
10322.4423192134.8834.88
1126222430.7330.73
122527--
132830--
1431
Sheet1
</f4),(qty-(calccumulative></f4)*if(size<if(qty<if(qty-(cumulative-f4)<qty-(f3-prevcumulative),qty-(cumulative-f4),qty-(f3-prevcumulative)),qty,if(qty-(cumulative-f4)<qty-(f3-prevcumulative),qty-(cumulative-f4),qty-(f3-prevcumulative))),size,if(qty<if(qty-(cumulative-f4)<qty-(f3-prevcumulative),qty-(cumulative-f4),qty-(f3-prevcumulative)),qty,if(qty-(cumulative-f4)<qty-(f3-prevcumulative),qty-(cumulative-f4),qty-(f3-prevcumulative)))))>
 
Last edited:
Upvote 0
Could you please finally attach your excel sheet which use range for "n" price and "n" quantity.

I dont want VBA solution..

Another small modification (with a consolidation of my previous suggestions):

Here is my file: MrE - QtyPrice - https://1drv.ms/x/s!AvFxmZVmmL9Sh3OOo1PVhT8ZslOm

1) Create the names below:

Qty - Refers to: =CalcQty!$A$2:INDEX(CalcQty!$A:$A,MATCH(8^7,CalcQty!$A:$A,1))
Prices - Refers to: =CalcQty!$B$2:INDEX(CalcQty!$B:$B,MATCH(8^7,CalcQty!$A:$A,1))
Size - Refers to: =CalcQty!$E$1
FromTo - Refers to: =CalcQty!$D$4:INDEX(CalcQty!$D:$D,MATCH("ZZZ",CalcQty!$D:$D,1))
RowQty - Refers To: =ROW(INDIRECT("1:"&ROWS(Qty)))
Cumulative - Refers To: =MMULT(--(RowQty>=TRANSPOSE(RowQty)),Qty)
PrevCumulative - Refers To: =MMULT(--(RowQty-1>=TRANSPOSE(RowQty)),Qty)
SizeN - Refers to: =ROW()-ROW(INDEX(FromTo,1))+1
SizeN_1 - Refers to: =ROW()-ROW(INDEX(FromTo,1))
CalcCumulative - Refers to: =Cumulative-SizeN
CalcPrevCumulative - Refers to: =SizeN_1-PrevCumulative

2) Put the formula below in D4 and copy down:

=IF(Size*ROWS(D$4:D4)>SUM(Qty),"",Size*(ROWS(D$4:D4)-1)+1&"-"&Size*ROWS(D$4:D4))

3) Put the formula below in E4 and copy down:

=IF(Size*ROWS(D$4:D4)>SUM(Qty),"",SUMPRODUCT(Prices,--(Cumulative>SizeN_1),--(PrevCumulative < SizeN),
(Qty-(CalcCumulative>0)*CalcCumulative-(CalcPrevCumulative>0)*CalcPrevCumulative))/Size)

<sizen),


Important: to my suggestion to work properly you must avoid putting other data
(in addition to the original data of the respective column) below the columns A, B and D.



[TABLE="class: grid, width: 413"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[TD]E[/TD]
[TD]F[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Quantity[/TD]
[TD]Price[/TD]
[TD][/TD]
[TD]Group Size[/TD]
[TD]3[/TD]
[TD]Sheet[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]4[/TD]
[TD]34,00[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]CalcQty[/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
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]15[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]16[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]****[/TD]
[TD]********[/TD]
[TD]********[/TD]
[TD]***[/TD]
[TD]***********[/TD]
[TD]*********[/TD]
[TD]*********[/TD]
[/TR]
</tbody>[/TABLE]

Markmzz</sizen),
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
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