How to count repeating values
This article demonstrates ways to count contiguous values in a column, in other words, values that repeat and are adjacent.
What's on this webpage
1. How to count any contiguous value
The formula in cell D4 checks if the value in cell D3 is not equal to the value in cell D4. If true (not equal) the result is 1. If false (equal) the formula adds 1 to the previous value above.
Copy cell D4 and paste to cells below as far as needed in order to count all values in column B.
Formula in cell D4:
The formula in cell F3 simply extracts the largest number from cell range D3:D14. Adjust cell ref D3:D14 if you have more values than the example shown in the image above.
Formula in cell F3:
Explaining formula in cell D4
Step 1 - Logical test
Combine the less than character and the larger than character and you get not equal to. The result is a boolean value TRUE or FALSE.
The first argument in the IF function is the logical test, it determines what value to return.
B4<>B3
becomes
"C"<>"A"
and returns TRUE.
Step 2 - Evaluate IF function
The IF function returns one value if the logical test returns TRUE and another value if the logical test returns FALSE.
IF(logical_test, [value_if_true], [value_if_false])
IF(B4<>B3, 1, D3+1)
becomes
IF(TRUE, 1, D3+1)
and returns 1 in cell D4.
2. How to count a specific contiguous value
David asks:
Hi Oscar,
In column A, I have a long random list of two variables, "N/A" and the value 1. In column B I want to identify the number of contiguous occurrences of the value 1 before the next appearance of "N/A".
I wonder if you could point me to the best way of achieving this, please?
Example below shows what I am after.Much appreciation for your excellent website,
David
Col A Col B
N/A
N/A
1
1
1
1
1 5
N/A
1
1
1
1 4
N/A
N/A
1
1 2
N/A
N/A
N/A
1 1
Thanks, David.
Formula in cell D4:
Explaining formula in cell D4
Step 1 - Logical test
Combine the less than character and the larger than character and you get not equal to. The result is a boolean value TRUE or FALSE.
The first argument in the IF function is the logical test, it determines what value to return.
B4<>B3
becomes
"N/A"<>"N/A"
and returns FALSE.
Step 2 - Evaluate IF function
The IF function returns one value if the logical test returns TRUE and another value if the logical test returns FALSE.
IF(logical_test, [value_if_true], [value_if_false])
IF(B4<>B3, 1, D3+1)
becomes
IF(FALSE, 1, D3+1)
becomes
D3+1
becomes
"" + 1
and returns 1.
Step 3 - Nested IF function
The second IF function makes sure that the value in cell B4 is equal to 1, we want to only count cells equal to 1.
IF(B4=1, IF(B4<>B3, 1, D3+1), "")
becomes
IF("N/A"=1, IF(B4<>B3, 1, D3+1), "")
becomes
IF(FALSE, IF(B4<>B3, 1, D3+1), "")
becomes
IF(FALSE, 1, "")
and returns "" (nothing) in cell D4.
Read Rick Rothsteins's comment below.
If you want to learn more about array formulas join my Advanced excel course.
Count values category
This post demonstrates how to build an array formula that counts unique distinct values based on criteria. What's on this […]
This article explains how to count cells highlighted with Conditional Formatting (CF). The image above shows data in cell range […]
This article describes how to count unique distinct values. What are unique distinct values? They are all values but duplicates are […]
This article demonstrates formulas that calculate the number of cells between two values, the first scenario involves two search values […]
Formula in C12: =SUM(1/COUNTIF($B$3:$B$8, $B$3:$B$8))+SUM(IF(COUNTIF($B$3:$B$8, $D$3:$D$8)=0, 1/COUNTIF($D$3:$D$8, $D$3:$D$8), 0)) How to create an array formula Double press with left mouse […]
This article demonstrates a formula that counts unique distinct values filtered from an Excel defined Table. Debra Dalgleish described in […]
The array formula in cell D3 calculates the number of unique distinct items based on the given date in column B. […]
The array formula in cell E3 counts unique distinct items for all dates within the same week. Example, week 2 […]
This article demonstrates how to construct a formula that counts unique distinct values based on a condition. The image above […]
This article demonstrates formulas that count values in cell based on a delimiting character. The image above shows a formula […]
The MEDIAN function lets you count overlapping dates between two date ranges. If you have more than two date ranges […]
This post demonstrates a formula in cell D16 that counts overlapping dates across multiple date ranges. The date ranges are […]
davidlim asks: re: Count records between two dates and a criterion based on the example, i was looking for 1 […]
This article demonstrates techniques on how to count cells based on the background color. I will also demonstrate user defined […]
The formula in cell E16 counts the number of cells between value B and H, Value B is in cell B3 […]
In the previous post I explained how to count overlapping dates comparing a single date range against multiple date ranges. […]
Question: My issue is that I get the date in this format: 7/23/2011 7:00:00 AM I am trying to count […]
NETWORKDAYS function returns the number of whole workdays between two dates, however the array formula I am going to demonstrate […]
Janib Soomro asks: In A column, there are dates in mmddyyy format and in B column, there are two variables […]
Functions in this article
More than 1300 Excel formulas
Excel formula categories
Excel categories
4 Responses to “How to count repeating values”
Leave a Reply
How to comment
How to add a formula to your comment
<code>Insert your formula here.</code>
Convert less than and larger than signs
Use html character entities instead of less than and larger than signs.
< becomes < and > becomes >
How to add VBA code to your comment
[vb 1="vbnet" language=","]
Put your VBA code here.
[/vb]
How to add a picture to your comment:
Upload picture to postimage.org or imgur
Paste image link to your comment.
First off, you show your formula starting in cell B3... you need to adjust it so that it starts in cell B2, otherwise you will not show a 1 in cell B2 if cell A2 differs from cell A3.
Second, here is a much simpler, still array-entered, formula, placed in cell B2 and copied down, that appears to produce the same output as your formula...
=IF(A3<>A4,ROW()-MAX(IF(A$1:A2<>A3,ROW(A$1:A2))),"")
Sorry, I posted the wrong formula, plus it looks like the comment processor ate my less than greater than symbol. Here is the correct array-entered formula, rearrange to eliminate the display problem...
=IF(A2=A3,"",ROW()-MAX(IF(A$1:A1<>A2,ROW(A$1:A1))))
[email protected]! I missed that the comment processor ate a second less than, greater than symbols in my formula. Here now is the correct [b]array-entered[/b] formula (rearranged so that there are no less than, greater than symbols at all)..
=IF(A2=A3,"",ROW()-MAX(IF(A$1:A1=A2,,ROW(A$1:A1))))
Rick Rothstein (MVP - Excel),
Your formulas work fine, thanks. So much better and smaller than mine.
I am sorry for wordpress eating your less/greater than signs, I try to edit your comments as soon as I can.