Calculate Values Until a Condition is Met

liquidlightning

New Member
Joined
Nov 16, 2021
Messages
4
Office Version
  1. 2016
Platform
  1. Windows
Hey all,

I have a spreadsheet with landmarks for an upcoming long run listed, and I'd like to have Excel calculate the mileage until the next available water source. I've provided a simplified version of the spreadsheet below. I'm comfortable using basics commands, like IF, AND, COUNT, etc., but upon browsing some threads I was quickly in over my head. The LOOKUP and INDEX commands legitimately intimidate me.

Essentially I want Column D to calculate miles until the next "Y" in Column C. It's easy enough to do myself on a small enough range, but where's the satisfaction in that?

Sample.png


Thanks!
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
I should mention that I did try to sort it out myself before realizing this may be trickier than anticipated. For D2 I tried =IF(C3="Y",B3-B2,... but then I got lost. I don't want to just endlessly build =IF commands until the next instance of "Y" in Column C.
 
Upvote 0
Welcome to the Forum!

ABC
1MilesWaterMiles to water
203
312
421
53Y0
645
754
863
972
1081
119Y0
12105
13114
14123
15132
16141
1715Y0
1816-
Sheet1
Cell Formulas
RangeFormula
C2:C18C2=IFERROR(INDEX(A2:A$18,MATCH("Y",B2:B$18,))-A2,"-")


If the mileposts are always sequential 0, 1, 2, 3, 4 ... etc, you could simplify to
C2: =IFERROR(MATCH("Y",B2:B$18,)-1,"-")
 
Upvote 0
Thanks for the reply!

That helped for the majority, but I guess I'd prefer if the rows marked Y didn't return as 0 and would instead calculate miles until next water source. That way if I'm standing at a water source, I can check my printed table and know how long until I'll be at water again. Here's a screenshot of the actual spreadsheet if that adds context.

1637163390569.png
 
Upvote 0
How about

Yes! That's it.

I tried adjusting the MATCH array to exclude the current row, but it wasn't coming out right. The +1 parameter fixed it. Perfect!

Now that I know which way is up I'll spend some time familiarizing myself with the arguments used here for future applications.

Thanks both!
 
Upvote 0

Forum statistics

Threads
1,214,428
Messages
6,119,420
Members
448,895
Latest member
omarahmed1

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