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

Checking for Either of Two Text Values

Using a formula to find information in a text value is easy. Using a formula to find either of two text values within a ...

Discover More

Repeating Your Typing

Want a quick way to repeat a word or phrase you just typed? Here's the shortcut you need.

Discover More

Deleting a Power Management Plan

Windows allows you to create custom power management plans. When you no longer need a plan you previously defined, you ...

Discover More

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!

More ExcelTips (ribbon)

Calculating the Median Age of a Group of People

Suppose you have a worksheet that contains a list of ages and then a count of people who correspond with those ages. You ...

Discover More

Generating Random Strings of Characters

If you need to generate a random sequence of characters, of a fixed length, then you'll appreciate the discussion in this ...

Discover More

Applying Range Names to Formulas

If you define your named ranges after you create your formulas, you can have Excel update those formulas to reflect the ...

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 three less than 3?

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.