Written by Allen Wyatt (last updated October 18, 2025)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Excel in Microsoft 365
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.
Provided you are using Excel 2021, 2024, or Microsoft 365, the calculation is rather easy to do using the XMATCH function. Let's assume that Roger's values in column A begin in cell A1. You could put this formula in cell B2:
=XMATCH(A2,A$1:A1,0,-1)
It compares cell A2 with the range of cells in A1:A1, searching upwards. This may not seem very useful, but if you copy it down column B, what you end up with is the comparison range growing so that XMATCH is always comparing the value to the left with the range of cells above it, search up the column. Thus, if the formula is copied to cell B351, it will look like this:
=XMATCH(A351,A$1:A350,0,-1)
This is much handier, with one exception: XMATCH returns the row in which the last occurrence occurred, or it returns an #N/A value. Roger doesn't want the row, he wants the interval between the current row and the previous row. He certainly doesn't want #N/A values, either. So, the solution is to test for the #N/A value and, if it is found, return a blank. If a row is returned, then calculate the interval. That is done by placing the following in cell B2:
=IF(ISNA(XMATCH(A2,A$1:A1,0,-1)),"",ROW(A2)-XMATCH(A2,A$1:A1,0,-1))
If you enlist the help of the LET function, you can make the formula even shorter:
=LET(a,XMATCH(A2,A$1:A1,0,-1),IF(ISNA(a),"",ROW(A2)-a))
If you are using a version of Excel that doesn't include XMATCH (it was introduced in Excel 2021), then Roger's task becomes much more difficult because there was no way to search backwards, up a column. If the premise could be reversed, then the task becomes much simpler. For instance, if a formula in B246 could return the value 105, indicating the interval until the next occurrence of the value in cell 246, instead of calculating the last occurrence. The following formula, placed in cell B1, indicates the next occurrence of the value in cell A1:
=MATCH(A1,A2:A$10000,0)
Copy the formula 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$10000,0)),0,MATCH(A1,A2:A$10000,0))
Remember, in the above examples, that you may need to adjust the row indicator (10000) to something that reflects the largest row number you anticipate.
If you absolutely must count upwards and you cannot use XMATCH, 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 Variant Dim X As Long Application.Volatile RowInterval = "" If LookHere.Columns.Count > 1 Then RowInterval = "Too many columns" Else For X = LookHere.Rows.Count To 1 Step -1 If LookHere.Rows(X).Cells(1) = TestCell Then RowInterval = TestCell.Row - X Exit For End If Next X End If End Function
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)
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (10258) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Calculating the Interval between Occurrences.
Program Successfully in Excel! This guide will provide you with all the information you need to automate any task in Excel and save time and effort. Learn how to extend Excel's functionality with VBA to create solutions not possible with the standard features. Includes latest information for Excel 2024 and Microsoft 365. Check out Mastering Excel VBA Programming today!
Suppose you have a worksheet that contains a list of ages and then a count of people who correspond with those ages. You ...
Discover MoreIf you need to generate a random sequence of characters, of a fixed length, then you'll appreciate the discussion in this ...
Discover MoreIf you define your named ranges after you create your formulas, you can have Excel update those formulas to reflect the ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2025-10-18 15:23:48
Pieter
It is very possible to di this in older versions of Excel, using an array formula.
For example, assuming the items are in column A, starting in A2 downwards (With the column header in A1).
In cell B3 enter the formula:
{=IFERROR(Row()-1-MAX(MATCH(A3,$A$2:A2,0)),"")}
(the {} braces are automatically added by pressing ctrl-shift-enter for entering the array formula)
Now copy cell B3 down to the last filled row and you have the offsets to the last previous occurrence.
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 © 2025 Sharon Parq Associates, Inc.
Comments