Finding max value in dynamic range

hedgie

Board Regular
Joined
Jun 23, 2004
Messages
174
Office Version
  1. 365
Platform
  1. Windows
Here is what I have data wise - Note that I am fudging Column G formulas, that is not the issue. What I want to do is find the Max value of Column G for each range of stock prior to the qty being 0 in column F. So in other words in H2 I would want to see 3735. In h7 I want to see 50, in h9 = 5120 and then in h14 =1850. I would like the other cells in column H to remain blank. Then I can sort to find the max cost for each security during the time period we held it. Any thoughts?

Book1
ABCDEFG
1DateNameSideQTYValueTotal SharesTotal Cost
23/1/2021 ABC B100250100250
34/2/2021 ABC B50010106001260
45/1/2021 ABC B1000210016003360
55/15/2021 ABC B20037518003735
66/22/2021 ABC S1800360000
75/1/2021 XYZ B2005020050
84/1/2022 XYZ S100751000
93/1/2021 DEF B10010001001000
103/15/2021 DEF B20020203003020
113/21/2021 DEF S1009502000
124/18/2021 DEF B20021004005120
1312/15/2021 DEF S400410000
144/7/2021 DEF B20018502001850
Sheet1
Cell Formulas
RangeFormula
G2:G11,G13:G14G2=IF(AND(C2="B",B2=B1),G1+E2,IF(C2="B",E2,0))
F2:F14F2=IF(B2=B1,F1+IF(C2="B",D2,-D2),D2)
 
I would consider using a helper column for this...or using VBA. In this example, I've added a helper column (col. I) that contains the logic used to indicate subgroups whose maxima are to be determined. The data table is assumed to be sorted such that each type of stock is grouped together, and then within each stock grouping, the transactions are in ascending data order. Then the subgroups shown in the helper column are defined by either a change in stock or when a 0 is encountered in column F. Those subgroups can be easily examined to confirm that they make sense. Then in column H, the FILTER function is used in conjunction with the helper column I subgroup to extract the Total Cost array for that subgroup and the MAX is taken. Regarding the point mentioned about assumptions, should the transaction in yellow be situated between the transactions in orange? A quick note about the helper column: in this case, it cannot be labeled in I1 as that will produce an error, so I1 should be left blank...the column can, however, be hidden.
MrExcel_20220426.xlsm
ABCDEFGHI
1DateNameSideQTYValueTotal SharesTotal CostMax
23/1/2021 ABC B10025010025037351
34/2/2021 ABC B50010106001260 1
45/1/2021 ABC B1000210016003360 1
55/15/2021 ABC B20037518003735 1
66/22/2021 ABC S1800360000 1
75/1/2021 XYZ B2005020050502
84/1/2022 XYZ S100751000 2
93/1/2021 DEF B1001000100100051203
103/15/2021 DEF B20020203003020 3
113/21/2021 DEF S1009502000 3
124/18/2021 DEF B20021004005120 3
1312/15/2021 DEF S400410000 3
144/7/2021 DEF B2001850200185018504
Sheet5 (2)
Cell Formulas
RangeFormula
H2:H14H2=IF($I2<>$I1,MAX(FILTER($G$2:$G$14,$I$2:$I$14=$I2)),"")
I2:I14I2=IF(OR(F1=0,B2<>B1),I1+1,I1)
G2:G11,G13:G14G2=IF(AND(C2="B",B2=B1),G1+E2,IF(C2="B",E2,0))
F2:F14F2=IF(B2=B1,F1+IF(C2="B",D2,-D2),D2)
 
Upvote 0

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

Forum statistics

Threads
1,224,587
Messages
6,179,735
Members
452,939
Latest member
WCrawford

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