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

Calculating the Interval between Occurrences

Written by Allen Wyatt (last updated June 26, 2020)
This tip applies to Excel 97, 2000, 2002, and 2003


4

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)

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 (2338) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) 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

Using a Macro to Change the Formatting of All Instances of a Word

If you have a word that you need to make sure is formatted the same way throughout your document, there are several ways ...

Discover More

Adding Text to a Drawing Shape

You can add text to all sorts of drawing shapes, not just text boxes. Here's how easy it is.

Discover More

Defining a Single Conditional Formatting Condition

Conditional formatting is a powerful tool you can use to dynamically adjust the formatting of your worksheet. This tip ...

Discover More

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!

More ExcelTips (menu)

Counting Odds and Evens

If you have a series of values in a range of cells, you may wonder how many of those values are even and how many are ...

Discover More

Finding the Directory Name

Need to know the directory (folder) in which a workbook was saved? You can create a formula that will return this ...

Discover More

Using Named Formulas or Constants

An easy way to create a name for a formula or constant value. The name can then be used in other formulas or for ...

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 four minus 0?

2019-01-15 23:04:30

Fred Burg

I seem to have fallen off this email list.


2019-01-13 18:14:44

kevin jackson

Your example only counts interval based on 1 column of numbers, what if there are 3 columns?

4 9 10
8 16 24
9 14 15
23 25 27

In the array above, number 9 shows in position 2 in row 1 and shows in position 1 in row 3.

is there a way to count rows to last occurrence of number 9, taking into account all numbers in a row, ie-regardless where 9 shows up in the row?



2015-12-06 10:09:37

Michael (Micky) Avidan

To my opinion (if I manage to correctly understand the task) the suggested "Array Formula" (No helper column) in the attached linked picture should do.
http://screenpresso.com/=nBUje
--------------------------
Michael (Micky) Avidan
“Microsoft® Answers" - Wiki author & Forums Moderator
“Microsoft®” MVP – Excel (2009-2016)
ISRAEL


2015-12-05 10:28:25

Fred Burg

Hi Allen,

It's not totally clear what Roger needs. Can the contents of A246 and A351 change?

For example, suppose A351 contains the word "Class" and the last time that occurred was in A246 and neither cell would change.

If that's the case, this is easy. If A351 can change to, for example, "Rooms" and Roger now wants to know when that last occurred, that is harder.

Going back to my first scenario (which I actually use), add a hidden column B with a formula such as
=if(A1="Class",ROW(),0)
and fill down.

Then to get the last occurrence of a row with "Class" above the current row - let's say above A351, just use
=max(B$1,B350).

and the number of rows between the 2 occurrences (the current row and the previous row with "Class") is just
=Row() - max(B$1,B350)


The alternative to allow for this to be more dynamic (the values in A246 and A351 can change) would take a little more work.

Fred


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon 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.