VBA Macro to Expand Selection to Adjacent Cells With Same Value

nekosuke

New Member
Joined
May 20, 2013
Messages
2
Hi guys!
I'm having a tough problem. When I select a cell in a table with a certain value, I want all the cells adjacent to that selected cell with the same value to also be selected. After that, the cells adjacent to the newly selected cells that have the same value are also selected.

Can anyone please tell me how this is done on VBA? What is the code for this? I've been trying things for hours, but I haven't even come close. Thanks! :)
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Welcome to the MrExcel board!

1. Can you give us some idea of where the top left of the table is and about how many rows/columns?

2. If, say, C4 and D5 contain the same value, do you consider them adjacent? That is, are touching diagonal corners considered adjacent?

3. Are there blank cells in the table? If so, how do we determine where the edges of the table are?
 
Upvote 0
Welcome to the MrExcel board!

1. Can you give us some idea of where the top left of the table is and about how many rows/columns?

2. If, say, C4 and D5 contain the same value, do you consider them adjacent? That is, are touching diagonal corners considered adjacent?

3. Are there blank cells in the table? If so, how do we determine where the edges of the table are?


1. Our table size is Range(Cells(2,2), Cells(17,13))

2. No, the adjacent cells are only vertical and horizontal.

3. We already gave a definite value for the size of our table. The cells are actually coloured and they contain their corresponding .Interior.ColorIndex value. :)
 
Upvote 0
Let's see if this does what you want.
To implement ..

1. Right click the sheet name tab and choose "View Code".

2. Copy and Paste the code below into the main right hand pane that opens at step 1.

3. Close the Visual Basic window & test.

Note that the code assumes that the main table (B2:M17 in your case) does NOT border a worksheet boundary.
That is, if the main table starts in column A or row 1, the code will error in some circumstances.
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  Dim Tbl As Range, Ctr As Range, Rng As Range, Rtmp As Range, Ctest As Range, c As Range
  Dim v
  Dim Found As Boolean

  Set Tbl = Range("B2:M17")
  Set Ctr = ActiveCell
  If Not Intersect(Ctr, Tbl) Is Nothing Then
    Set Rtmp = Ctr
    v = Ctr.Value
    Do
      Set Rng = Rtmp
      Found = False
      For Each c In Rng
        For Each Ctest In Union(c.Offset(-1), c.Offset(1), _
                            c.Offset(, -1), c.Offset(, 1))
          If Not Intersect(Ctest, Tbl) Is Nothing Then
            If Intersect(Ctest, Rng) Is Nothing Then
              If Ctest.Value = v Then
                Set Rtmp = Union(Rtmp, Ctest)
                Found = True
              End If
            End If
          End If
        Next Ctest
      Next c
    Loop While Found
    Application.EnableEvents = False
    Rng.Select
    Ctr.Activate
    Application.EnableEvents = True
  End If
End Sub

As an example, if any one of the green cells below is selected, all those green cells will be selected. I hope that is what you meant.

Excel Workbook
ABCDEFGHIJKLMN
1
2433224145413
3542521523413
4324422553552
5452325413114
6212225532114
7334242131352
8422544313455
9135343332321
10251223132431
11543214343512
12112113435114
13321453143332
14213151325424
15211242232213
16253452314351
17542132511235
18
Adjacent Cells
 
Upvote 0
Peter, I'm making some basic games for fun in excel, trying to learn a thing or two.
Found great help in your code, thanks, as i am making a game of Go.
With your code i check if the cells adjacent to where the last move was contains a chain that is alive or dead.
 
Upvote 0

Forum statistics

Threads
1,214,956
Messages
6,122,465
Members
449,085
Latest member
ExcelError

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