Formula to count number of matching contiguous cells?

alexcr

Board Regular
Joined
Oct 15, 2018
Messages
59
Office Version
  1. 365
Platform
  1. Windows
Let's say there are 10 cells in a row...

Apple, Apple, Banana, Banana, Banana, Apple, Orange, Orange, Orange, Orange

In the next row, you want to automatically calculate the number of *contiguous* cells that match the cell above, a formula which should produce...

2, 2, 3, 3, 3, 1, 4, 4, 4, 4

Note that this would be different than calculating the total number of cells in the row that match the cell above. For example, even though "Apple" appears three times in total, in the first contiguous block of "Apple", it appears twice, and in the second contiguous block of "Apple", it only appears once.

What would be the best formula to accomplish this?a
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Not exactly sure of our layout, but does this help?

22 07 22.xlsm
ABCDEFGHIJKL
1AppleAppleBananaBananaBananaAppleOrangeOrangeOrangeOrange
22233314444
3
Sheet5
Cell Formulas
RangeFormula
B2:K2B2=IF(B1=A1,A2,MATCH(TRUE,B1:K1<>C1:L1,0))
 
Upvote 0
Solution
Thank you, Peter! Your formula seems to work well. While looking at it, I came up with another approach, albeit a two-row one, that also seems to work.

In cell B2, I entered the formula =IF(A1=B1,A2+1,1). I then copied this down its row.

In cell B3, I entered the formula =IF(B1=C1,C3,B2). I then copied this down its row.
 
Upvote 0
You're welcome. Glad to help. :)

Yes, your method will also work if you don't mind using the helper row.
 
Upvote 0

Forum statistics

Threads
1,214,812
Messages
6,121,696
Members
449,048
Latest member
81jamesacct

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top