Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Excel in Microsoft 365. 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.

Calculating the Interval between Occurrences

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


1

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:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

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.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

E-mailing PDF Reports Results in Consistent Crash

It is possible to create macros that send out reports, via e-mail, from within Excel. Frank did this and ran into ...

Discover More

Only Showing the Maximum of Multiple Iterations

When you recalculate a worksheet, you can determine the maximum of a range of values. Over time, as those values change, ...

Discover More

Replacing and Renumbering

Word has a powerful find and replace capability that can help you perform quite a few changes to your documents. This tip ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2019 For Dummies today!

More ExcelTips (ribbon)

Counting Consecutive Negative Numbers

If you have a range of values that can be either positive or negative, you might wonder how to determine the largest ...

Discover More

Combinations for Members in Meetings

Got a large group of people listed in a worksheet and you want to make sure that each person has met with every other ...

Discover More

Identifying Values that Don't Follow a Specific Pattern

When you store textual information in a worksheet, it can be helpful to figure out if that information follows a pattern ...

Discover More
Subscribe

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

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 5 - 4?

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.


This Site

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.

Newest Tips
Subscribe

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

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.