Count all adjoining cells with the same value

rubbis_with_excel

New Member
Joined
Jan 19, 2004
Messages
28
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

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
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
 
Upvote 0
Hi, thanks for the reply.

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!!!
 
Upvote 0

Forum statistics

Threads
1,214,657
Messages
6,120,773
Members
448,991
Latest member
Hanakoro

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