Lookup value until cell in reference column changes

tvrage

New Member
Joined
May 24, 2017
Messages
2
Dear experts,

I have random securities in column B with a respective value in column C. I want to calculate the profit I made if I sell the security. However, I only sell if the security in column B changes in the following row. Otherwise I hold the security and the profit is zero (or empty, not relevant). Here is an example. I start with 100 and the periods increase with rows, i.e. I compute profit by calculating from bottom to top.

[TABLE="class: grid"]
<tbody>[TR]
[TD]Month (=A1)[/TD]
[TD]Security[/TD]
[TD]Value[/TD]
[TD]Profit[/TD]
[TD]Calculation[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD]100[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]30/09/2011[/TD]
[TD]Bond[/TD]
[TD]91.49[/TD]
[TD]0[/TD]
[TD]-[/TD]
[/TR]
[TR]
[TD][TABLE="width: 84"]
<tbody>[TR]
[TD="class: xl65, width: 84, align: right"]31-Oct-2011[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD]Bond[/TD]
[TD]91.65[/TD]
[TD]0[/TD]
[TD]-[/TD]
[/TR]
[TR]
[TD][TABLE="width: 84"]
<tbody>[TR]
[TD="class: xl65, width: 84"]30-Nov-2011[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD]Bond[/TD]
[TD]91.80[/TD]
[TD]0[/TD]
[TD]-[/TD]
[/TR]
[TR]
[TD][TABLE="width: 84"]
<tbody>[TR]
[TD="class: xl65, width: 84"]31-Dec-2011[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD]Bond[/TD]
[TD]91.95[/TD]
[TD]0[/TD]
[TD]-[/TD]
[/TR]
[TR]
[TD][TABLE="width: 84"]
<tbody>[TR]
[TD="class: xl65, width: 84, align: right"]31-Jan-2012[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD]Bond[/TD]
[TD]92.10[/TD]
[TD]-7.90[/TD]
[TD]C7-C2[/TD]
[/TR]
[TR]
[TD][TABLE="width: 84"]
<tbody>[TR]
[TD="class: xl65, width: 84"]29-Feb-2012[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD]S&P[/TD]
[TD]95.67[/TD]
[TD]3.56[/TD]
[TD]C8-C7[/TD]
[/TR]
[TR]
[TD][TABLE="width: 84"]
<tbody>[TR]
[TD="class: xl65, width: 84"]31-Mar-2012[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD]Dow[/TD]
[TD]96.92[/TD]
[TD]0[/TD]
[TD]-[/TD]
[/TR]
[TR]
[TD][TABLE="width: 84"]
<tbody>[TR]
[TD="class: xl65, width: 84, align: right"]30-Apr-2012[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD]Dow[/TD]
[TD]94.32[/TD]
[TD]-1.35[/TD]
[TD]C10-C8[/TD]
[/TR]
</tbody>[/TABLE]


I want to find the formula for the profit. The table continues for various periods, so the securities repeat themselves randomly. I tried to use INDEX with counting the number of time a security appears until it changes but it did not work. COUNTIF until value in column B changes didn't work either since it counts all values in the entire table, not just the ones until the security changes.

I could not find a forum entry in this respect.
Many thanks for your help.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.

Book1
ABCD
1MonthSecurityValueProfit
2100.00
330/09/2011Bond91.490.00
431/10/2011Bond91.650.00
530/11/2011Bond91.800.00
631/12/2011Bond91.950.00
731/01/2012Bond92.10-7.90
829/02/2012S&P95.673.57
931/03/2012Dow96.920.00
1030/04/2012Dow94.32-1.35
Sheet1
Cell Formulas
RangeFormula
D3{=IF($B3=$B4,0,$C3-INDEX($C$2:$C3,MAX(IF($B$2:$B3<>$B3,ROW($B$2:$B3)-1,0))))}
Press CTRL+SHIFT+ENTER to enter array formulas.


WBD
 
Upvote 0
A Non-Array solution of the same can be like this:



Unknown
ABCD
1Month (=A1)SecurityValueProfit
2100
330/09/2011Bond91.49-
431-Oct-11Bond91.65-
530-Nov-11Bond91.8-
631-Dec-11Bond91.95-
731-Jan-12Bond92.1-7.90
829-Feb-12S&P95.673.57
931-Mar-12Dow96.92-
1030-Apr-12Dow94.32-1.35
Sheet6
Cell Formulas
RangeFormula
D3=IF(B3<>B4,C3-INDEX($C$2:$C$10,MATCH(B3,$B$2:$B$10,0)-1),"-")
 
Upvote 0
Only if the value in Column B doesn't repeat. Switching it back to "Bond" in B9, for example, would make this formula fail.

WBD
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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