Formula or VBA for Cell Range of Values

ststern45

Well-known Member
Joined
Sep 17, 2005
Messages
974
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Hi everyone.

I have a range of 40 cells and in these 40 cells are a total of 4 values from 0 through 9.
The 40 cells will always have 4 values. No more no less.
If the 4 values in the 40 cells all touch either horizontal, vertically, etc. As long as each value touches one another.
Image 1:
Example where all 4 numbers touch each other.
The values 2-0-1-3 all touch. If the 4 numbers touch then a value of 1. If they don't a value of 0

Image 2:
1 or more numbers do not touch.
In this case the result would be false and a value of 0 (zero)
Even though the values 2, 0, and 5 touch the 4 does not so the result would be false of the value 0 (zero)

Thank you in advance!!
 

Attachments

  • MrExcel1.png
    MrExcel1.png
    2.6 KB · Views: 19
  • MrExcel2.png
    MrExcel2.png
    1.8 KB · Views: 21

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.
Maybe:

Book1
ABCDEFGHIJK
1P1P2P3P4All Touch?P1P2P3P4All Touch?
220131200
354
4
5
6
7
8
9
10
11
Sheet7
Cell Formulas
RangeFormula
E2,K2E2=LET(t,SMALL(IF(A2:D11<>"",ROW(A2:D11)*100+COLUMN(A2:D11),""),{1;2;3;4}),a,t+{-1,1,-100,100},b,SMALL(a,SEQUENCE(16)),PRODUCT(ISNUMBER(MATCH(t,b,0))+0))
 
Upvote 0
Thank you Eric.
Formula works great but I made an error in my initial post.
If a cell is touches diagonal, this should also be included.
See image
Thank you.
 

Attachments

  • MrExcel3.png
    MrExcel3.png
    2.2 KB · Views: 15
Upvote 0
In addition, this would also qualify since a values touch diagonally.

See image
Thank you
 

Attachments

  • MrExcel4.png
    MrExcel4.png
    2.4 KB · Views: 10
Upvote 0
OK, try:

Excel Formula:
=LET(t,SMALL(IF(A2:D11<>"",ROW(A2:D11)*100+COLUMN(A2:D11),""),{1;2;3;4}),a,t+{-1,1,-100,100,101,99,-101,-99},b,SMALL(a,SEQUENCE(32)),PRODUCT(ISNUMBER(MATCH(t,b,0))+0))

I should probably also mention that this won't work past column CU, but it can be adjusted if that's an issue.
 
Upvote 0
I thought Eric's formula solution was probably the way to go and his skill level is well beyond mine.
Since I did manage to break the lastest formula with the data arangement below, and the problem sounds like the definition of current region I thought I would provide a VBA option.

s/be 0
1710655159131.png


VBA option

VBA Code:
Sub TouchingValues()

    Dim ws As Worksheet
    Dim rngSrc As Range, rngResult
    Dim rngDest As Range, cellFound As Range
  
    Set ws = ActiveSheet
    With ws
        Set rngSrc = .Range("A2").Resize(10, 4)             '<--- Change this to the first Data cell ie left top corner below heading
        Set rngResult = rngSrc.Cells(1, 1).Offset(, 5)      '<--- Based on your image this is the offset to the result cell
        Set rngDest = .UsedRange.Cells(1, 1).Offset(, .UsedRange.Columns.Count + 2).Resize(rngSrc.Rows.Count, rngSrc.Columns.Count)
      
        rngDest.Value = rngSrc.Value
        On Error Resume Next
        Set cellFound = rngDest.Find(what:="*")
        On Error GoTo 0
        If cellFound Is Nothing Then
            MsgBox "No Data In Data area"
            Exit Sub
        End If
      
        If WorksheetFunction.CountA(cellFound.CurrentRegion) = 4 Then
            rngResult.Value = 1
        Else
            rngResult.Value = 0
        End If
      
        rngDest.Clear
      
        Dim resetUsedRange As Long
        resetUsedRange = ws.UsedRange.Row
        
    End With
  
End Sub
 
Upvote 0
All 4 values must touch vertically, horizontally, diagonally.
See image
Only 2 values touch in the image
Sorry for the reply.
Thank you
 

Attachments

  • MrExcel5.png
    MrExcel5.png
    2.1 KB · Views: 16
Upvote 0
I know what the problem is, but I don't know how to fix it right away. I'll think on it. It might be a few days since I'm traveling. In the meantime, see if Alex's code works for you.
 
Upvote 0

Forum statistics

Threads
1,223,895
Messages
6,175,257
Members
452,625
Latest member
saadat28

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