**Please Note: **
This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. If you are using an earlier version (Excel 2003 or earlier), *this tip may not work for you*. For a version of this tip written specifically for earlier versions of Excel, click here: Calculating the Interval between Occurrences.

Roger asked if there was a way to calculate the interval between occurrences of values in a list. For instance, he has several thousand numbers in column A. Looking at the value in cell A351, the last time that value occurred in the list was in cell A246. He would like a formula that could be placed in cell B351 and return 105, the difference between 351 and 246.

This approach is difficult to implement in Excel because Excel is not very good at searching backwards—up a column. If the premise could be reversed, then the task becomes much simpler. For instance, if a formula in cell B246 could return the value 105, indicating the interval until the next occurrence of the value in cell A246, instead of calculating the last occurrence. The following formula calculates the next occurrence of the value in cell A1:

=MATCH(A1,A2:$A$65536,0)

Place this formula in cell B1 and copy it down however many cells are necessary. If the value in column A does not occur again in the column, then the formula returns the #N/A error. If you would rather have the formula return 0, then the following works:

=IF(ISNA(MATCH(A1,A2:$A$65536,0)),0,MATCH(A1,A2:$A$65536,0))

If you absolutely must count upwards (find the previous occurrence instead of the next occurrence), then the easiest way to do it is with a user-defined function. The following function, RowInterval, will look backward through a range you specify and return the desired interval:

Function RowInterval(TestCell As Range, LookHere As Range) As Long Dim varValue As Variant Dim lngRow As Long Application.Volatile varValue = TestCell.Value 'Check for occurrences of the test value in the search range If WorksheetFunction.CountIf(LookHere, varValue) > 0 Then With LookHere 'Get the last row of the search range lngRow = .Row + .Rows.Count - 1 'Start with the last cell in the search range and work up Do Until .Item(lngRow, 1).Value = varValue lngRow = lngRow - 1 Loop End With 'Subtract the number of the row containing the found occurrence 'from the number of the row containing the test value RowInterval = TestCell.Row - lngRow End If End Function

In order to use the function, you would put the following formula in cell B2, and then copy the formula down the number of desired cells:

=RowInterval(A2,A$1:A1)

*ExcelTips* is your source for cost-effective Microsoft Excel training.
This tip (10258) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel here: **Calculating the Interval between Occurrences**.

**Comprehensive VBA Guide** Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out *Mastering VBA for Office 2010* today!

When you copy a formula from one cell to another, Excel normally adjusts the cell references within the formula so they are ...

Discover MoreWhen working with data in Excel, you might want to figure out which rows of data represent duplicates of other rows. If it ...

Discover MoreWhen processing huge amounts of data, it can be a challenge to figure out how to derive the aggregate values you need. This ...

Discover More**FREE SERVICE:** Get tips like this every week in *ExcelTips,* a free productivity newsletter. Enter your address and click "Subscribe."

2017-02-17 09:00:33

Michael (Micky) Avidan

Try: =MATCH(8,G:G)-MATCH(8,A:A)-1

--------------------------

Michael (Micky) Avidan

“Microsoft® Answers" - Wiki author & Forums Moderator

“Microsoft®” MVP – Excel (2009-2017)

ISRAEL

2017-02-16 20:04:26

Zac Lay

Example: A2 contains the number 18. G14 contains the number 18. There are 11 rows between occurrences. I would like to output that number to the cell this formula resides in. I tried MATCH but it is too limited.

Is there a way to do this?

2016-09-08 17:30:12

Curtis Z

I have data in M2:R1825

Any given cell in preceding or following rows may have duplicate data of any other cell in that range, just never on the same row.

I am trying to figure out a way to count the rows between occurrences, not counting the row of last occurrence or the row of current occurrence.

Example: M2 contains the number 18. Q14 contains the number 18. There are 11 rows between occurrences. I would like to output that number to the cell this formula resides in. I tried MATCH but it is too limited.

Thank you for any help.

Z

Got a version of Excel that uses the
ribbon interface (Excel 2007 or later)?
**This site is for you!** If you
use an earlier version of Excel, visit
our *ExcelTips* site focusing on the menu interface.

**FREE SERVICE:** Get tips like this every week in *ExcelTips,* a free productivity newsletter. Enter your address and click "Subscribe."

Copyright © 2017 Sharon Parq Associates, Inc.

## Comments