Formula to count number of matching contiguous cells?

alexcr

Board Regular
Joined
Oct 15, 2018
Messages
61
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

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
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,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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