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)
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
What version of Excel are you using? You mention, "Max value of Column G for each range of stock prior to the qty being 0 in column F", which only occurs twice in your example. Yet you describe four outputs desired?
 
Last edited:
Upvote 0
What version of Excel are you using? You mention, "Max value of Column G for each range of stock prior to the qty being 0 in column F", which only occurs twice in your example. Yet you describe four outputs desired?
I am on 365 so I assume the latest. Sorry if I was confusing - I want the maximum value for each security for each period we held it. So for stock ABC that is 3750, however for stock DEF it shoudl be 5120 and then 1850. If quantity never reaches Zero that means we still hold it and I still want the max value.
 
Upvote 0
I am on 365

I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
 
Upvote 0
I'm still not clear about your 1st post. Is column F used at all? Take XYZ as an example. There is no value of 0 in column F for XYZ, so why is a result expected? Did you mean to examine only 0's in column G, and then for that particular stock, look back in time to find the maximum total cost associated with it (without crossing any prior 0)?
 
Upvote 0
F is used to track the current position as of a certain date. The purpose is to be able to determine when a position is closed (equal to 0) so that we can then start measuring again if there are additional purchases after that date.
 
Upvote 0
Okay, so if there is no 0 in column F for XYZ that suggests the position is still open...so why would you expect a result of 50 in H7?
 
Upvote 0
The buy value for the purchase was 50 - we did sell 1/2 at a profit , but I want to see the total cost of the buys only
 
Upvote 0
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.
You seem to be missing the point of my question. I've quoted above what you said in your first post. You mention that when the quantity is 0 in column F, that should trigger an output of the maximum in column G for that particular holding. You then give examples that are not consistent with that description. One of those examples involves holding XYZ: there is no 0 in column F for XYZ, so why would you expect an output of the maximum?
 
Upvote 0
Let me try to explain. The purpose it to know the maximum value we paid in total for a security during a time period in which we held or still hold the position. So is we only bought a stock multiple times and never sold it would be the maximum value in column G. If we bought in different lots and then sold it all then it would be the maximum value of G until the quantity in F is 0. If after selling it all we then purchase it back then I need to know the maximum of this new set of purchases.
 
Upvote 0

Forum statistics

Threads
1,215,048
Messages
6,122,862
Members
449,097
Latest member
dbomb1414

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