# Count all adjoining cells with the same value

#### rubbis_with_excel

##### New Member
Hi,

I've been looking for some code or a formula to count surrounding cells with the same value. I've found some examples, but I need something a bit more complex so I was hoping someone clever here can help me.

I'm developing a silly little game and I want to be able to recognise the whole surrounding block of same-value cells

Using the example below, I want to be able to recognise/count the lower block of A's and delete them, but not the row of A's at the top (I hope that makes sense!).

A A A A
B B C C
B A A B C
B A A A B C
B A A A A B

Every code or formula I've found either doesn't pick up enough of the A's or finds absolutely everything!

Any help would be appreciated, thanks

### Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Perhaps:-
Code:
``````[COLOR="Navy"]Sub[/COLOR] MG20Feb59
[COLOR="Navy"]Dim[/COLOR] c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Dn [COLOR="Navy"]As[/COLOR] Range

[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] ActiveSheet.Range("A1").CurrentRegion
[COLOR="Navy"]If[/COLOR] Dn.Value = "A" And Not Dn.Row = 1 [COLOR="Navy"]Then[/COLOR]
c = c + 1: Dn.Value = ""
[COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] Dn
MsgBox "There were  " & c & " ""A'[COLOR="Green"][B]s"" Deleted"[/B][/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]``````
Regards Mick

That solution is super quick!!! However, when I said that the top row shouldn't get counted, I meant because it's not part of that continuous block of A's rather than to always exclude the top row.

I want to be able to identify every "A" cell that is touching another "A" cell, as long as there are more than 4 in a group

Maybe I'm over-thinking this too much!!!

Replies
4
Views
277
Replies
3
Views
244
Replies
6
Views
168
Replies
0
Views
100
Replies
8
Views
244

1,206,969
Messages
6,075,919
Members
446,170
Latest member
zzzz02

### 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.

### Which adblocker are you using?

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

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