How to count until a certain value?

CHeiring

New Member
Joined
Apr 4, 2023
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hi

I have a spreadsheet in which I have a Column in which a "1" indicated the beginning of a section of time and "2" indicates the end of this section.

I would like to be able to from top to bottom to go through column A (see Image), and the count the number of rows between the first time a cell contain "1" until a cell contains "2" - number of rows will equal duration. It should then continue for every "1" and "2". The result = number of rows between each "1" and "2" pair should show in column B. In Column C I have shown how I would like the results to be presented.

Is this possible to do in Excel with formulas?

1680600097952.png



Many thanks in advance

Cheers

Christian
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
try this

Book1
AB
1
2
3
4
51 
6 
7 
823
9 
101 
11 
12 
13 
14 
15 
1626
Sheet1
Cell Formulas
RangeFormula
B5:B16B5=IF(A5=2,LOOKUP(2,1/($A$1:A5=2),ROW($A$1:A5))-LOOKUP(2,1/($A$1:A5=1),ROW($A$1:A5)),"")
 
Upvote 0
Hi & welcome to MrExcel.
Another option
Fluff.xlsm
AB
1
2 
3 
4 
51 
6 
7 
823
9 
101 
11 
12 
13 
14 
15 
1626
17 
18 
Main
Cell Formulas
RangeFormula
B2:B18B2=IF(A2=2,ROW()-XMATCH(1,A$1:A1,0,-1),"")
 
Upvote 0
Solution
Hi Alan
Many thanks for taking time to help and this looks like it could work.

However, I can't get the formula to work if I copy it into my version of Excel (danish version) . In my version of Excel I can choose between 3 different "Lookup" - in danish "OPSLAG" - VOPSLAG, LOPSLAG or XOPSLAG. Also I am confused about the "/" in the LOOKUP argument.
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0
Glad we could help & thanks for the feedback.
Can I be allowed an additional question. The method suggested with Xmatch works well and gives me a number when relevant. This number I need to use for calculation, fx median duration of an episode. However Excel consider the value as a textstring even though the cells are formatted as numbers. If fx I want to sum the values or multiply I get an error-message
 
Upvote 0
Both the formulae posted here will return numbers & not text.
 
Upvote 0
Can I be allowed an additional question. The method suggested with Xmatch works well and gives me a number when relevant. This number I need to use for calculation, fx median duration of an episode. However Excel consider the value as a textstring even though the cells are formatted as numbers. If fx I want to sum the values or multiply I get an error-message
nevermind - figured it out my self. Turns out I had an If-statement combining numbers and text somewhere in the data flow towards the column I needed help with.
 
Upvote 0
Glad you sorted it & thanks for letting us know.
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,583
Members
449,089
Latest member
Motoracer88

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