Function findmatches()

Dim startx As Integer
Dim starty As Integer
Dim endx As Integer
Dim endy As Integer
Dim x As Integer
Dim y As Integer
Dim z As Integer
Dim searchx As Integer
Dim searchy As Integer
Dim countmatch As Integer
Dim inserted As Boolean

searchx = 21
searchy = 11

startx = 6
endx = 15 - 2
starty = 13
endy = 0
'find last row
For y = starty To 5000
    If endy = 0 Then
        If Cells(y, startx).Value <= 0 Then
            endy = y - 1
        End If
    End If
Next y

Worksheets("Data").Range("A:C").ClearContents

Dim r1x As Integer
Dim r1y As Integer
Dim r1result As Integer

For y = starty To endy
    For x = startx To endx
        countmatch = 0
        If Cells(y, x).Value = Cells(searchy, searchx).Value Then
            countmatch = countmatch + 1
        End If
        If Cells(y + 1, x).Value = Cells(searchy + 1, searchx).Value Then
            countmatch = countmatch + 1
        End If
        If Cells(y + 2, x).Value = Cells(searchy + 2, searchx).Value Then
            countmatch = countmatch + 1
        End If
        If Cells(y, x + 1).Value = Cells(searchy, searchx + 1).Value Then
            countmatch = countmatch + 1
        End If
        If Cells(y + 1, x + 1).Value = Cells(searchy + 1, searchx + 1).Value Then
            countmatch = countmatch + 1
        End If
        If Cells(y + 2, x + 1).Value = Cells(searchy + 2, searchx + 1).Value Then
            countmatch = countmatch + 1
        End If
        If Cells(y, x + 2).Value = Cells(searchy, searchx + 2).Value Then
            countmatch = countmatch + 1
        End If
        If Cells(y + 1, x + 2).Value = Cells(searchy + 1, searchx + 2).Value Then
            countmatch = countmatch + 1
        End If
        If Cells(y + 2, x + 2).Value = Cells(searchy + 2, searchx + 2).Value Then
            countmatch = countmatch + 1
        End If
    If countmatch > 3 Then
        'need to order
        inserted = False
        For z = 1 To 100
            If (Sheet2.Cells(z, 3) <= countmatch Or Sheet2.Cells(1, 3) = "") And inserted = False Then
                Worksheets("Data").Range(z & ":" & z).EntireRow.Insert
                Sheet2.Cells(z, 1) = y
                Sheet2.Cells(z, 2) = x
                Sheet2.Cells(z, 3) = countmatch
                inserted = True
              End If
        Next z
        
    End If

    Next x
Next y

End Function


Function PopulateResults()

    Dim y As Integer
    Dim x As Integer
    Dim noresults As Integer
    Dim resultlocx As Integer
    Dim resultlocy As Integer
    
    noresults = 21
    
    Range("U:AG").Interior.Color = RGB(255, 255, 255)
    Range("U16:AG48").ClearContents
    
    For y = 1 To noresults
        If Worksheets("Data").Cells(y, 3).Value > 0 Then
            'Pick result location
            Select Case y
            Case 1
                resultlocx = 21
                resultlocy = 16
            Case 2
                resultlocx = 26
                resultlocy = 16
            Case 3
                resultlocx = 31
                resultlocy = 16
            Case 4
                resultlocx = 21
                resultlocy = 21
            Case 5
                resultlocx = 26
                resultlocy = 21
            Case 6
                resultlocx = 31
                resultlocy = 21
            Case 7
                resultlocx = 21
                resultlocy = 26
            Case 8
                resultlocx = 26
                resultlocy = 26
            Case 9
                resultlocx = 31
                resultlocy = 26
            Case 10
                resultlocx = 21
                resultlocy = 31
            Case 11
                resultlocx = 26
                resultlocy = 31
            Case 12
                resultlocx = 31
                resultlocy = 31
            Case 13
                resultlocx = 21
                resultlocy = 36
            Case 14
                resultlocx = 26
                resultlocy = 36
            Case 15
                resultlocx = 31
                resultlocy = 36
            Case 16
                resultlocx = 21
                resultlocy = 41
            Case 17
                resultlocx = 26
                resultlocy = 41
            Case 18
                resultlocx = 31
                resultlocy = 41
            Case 19
                resultlocx = 21
                resultlocy = 46
            Case 20
                resultlocx = 26
                resultlocy = 46
            Case 21
                resultlocx = 31
                resultlocy = 46
            End Select
            
            'Populate results
            Cells(resultlocy, resultlocx).Value = Cells(Worksheets("Data").Cells(y, 1).Value, Worksheets("Data").Cells(y, 2).Value).Value
            If Cells(resultlocy, resultlocx).Value = Cells(11, 21).Value Then
                Cells(resultlocy, resultlocx).Interior.Color = RGB(255, 255, 0)
            Else
                Cells(resultlocy, resultlocx).Interior.Color = RGB(255, 255, 255)
            End If
            Cells(resultlocy, resultlocx + 1).Value = Cells(Worksheets("Data").Cells(y, 1).Value, Worksheets("Data").Cells(y, 2).Value + 1).Value
            If Cells(resultlocy, resultlocx + 1).Value = Cells(11, 22).Value Then
                Cells(resultlocy, resultlocx + 1).Interior.Color = RGB(255, 255, 0)
            Else
                Cells(resultlocy, resultlocx + 1).Interior.Color = RGB(255, 255, 255)
            End If
            Cells(resultlocy, resultlocx + 2).Value = Cells(Worksheets("Data").Cells(y, 1).Value, Worksheets("Data").Cells(y, 2).Value + 2).Value
            If Cells(resultlocy, resultlocx + 2).Value = Cells(11, 23).Value Then
                Cells(resultlocy, resultlocx + 2).Interior.Color = RGB(255, 255, 0)
            Else
                Cells(resultlocy, resultlocx + 2).Interior.Color = RGB(255, 255, 255)
            End If
            Cells(resultlocy + 1, resultlocx).Value = Cells(Worksheets("Data").Cells(y, 1).Value + 1, Worksheets("Data").Cells(y, 2).Value).Value
            If Cells(resultlocy + 1, resultlocx).Value = Cells(12, 21).Value Then
                Cells(resultlocy + 1, resultlocx).Interior.Color = RGB(255, 255, 0)
            Else
                Cells(resultlocy + 1, resultlocx).Interior.Color = RGB(255, 255, 255)
            End If
            Cells(resultlocy + 1, resultlocx + 1).Value = Cells(Worksheets("Data").Cells(y, 1).Value + 1, Worksheets("Data").Cells(y, 2).Value + 1).Value
            If Cells(resultlocy + 1, resultlocx + 1).Value = Cells(12, 22).Value Then
                Cells(resultlocy + 1, resultlocx + 1).Interior.Color = RGB(255, 255, 0)
            Else
                Cells(resultlocy + 1, resultlocx + 1).Interior.Color = RGB(255, 255, 255)
            End If
            Cells(resultlocy + 1, resultlocx + 2).Value = Cells(Worksheets("Data").Cells(y, 1).Value + 1, Worksheets("Data").Cells(y, 2).Value + 2).Value
            If Cells(resultlocy + 1, resultlocx + 2).Value = Cells(12, 23).Value Then
                Cells(resultlocy + 1, resultlocx + 2).Interior.Color = RGB(255, 255, 0)
            Else
                Cells(resultlocy + 1, resultlocx + 2).Interior.Color = RGB(255, 255, 255)
            End If
            Cells(resultlocy + 2, resultlocx).Value = Cells(Worksheets("Data").Cells(y, 1).Value + 2, Worksheets("Data").Cells(y, 2).Value).Value
            If Cells(resultlocy + 2, resultlocx).Value = Cells(13, 21).Value Then
                Cells(resultlocy + 2, resultlocx).Interior.Color = RGB(255, 255, 0)
            Else
                Cells(resultlocy + 2, resultlocx).Interior.Color = RGB(255, 255, 255)
            End If
            Cells(resultlocy + 2, resultlocx + 1).Value = Cells(Worksheets("Data").Cells(y, 1).Value + 2, Worksheets("Data").Cells(y, 2).Value + 1).Value
            If Cells(resultlocy + 2, resultlocx + 1).Value = Cells(13, 22).Value Then
                Cells(resultlocy + 2, resultlocx + 1).Interior.Color = RGB(255, 255, 0)
            Else
                Cells(resultlocy + 2, resultlocx + 1).Interior.Color = RGB(255, 255, 255)
            End If
            Cells(resultlocy + 2, resultlocx + 2).Value = Cells(Worksheets("Data").Cells(y, 1).Value + 2, Worksheets("Data").Cells(y, 2).Value + 2).Value
            If Cells(resultlocy + 2, resultlocx + 2).Value = Cells(13, 23).Value Then
                Cells(resultlocy + 2, resultlocx + 2).Interior.Color = RGB(255, 255, 0)
            Else
                Cells(resultlocy + 2, resultlocx + 2).Interior.Color = RGB(255, 255, 255)
            End If
        
        
        End If

    
    Next y

End Function                                                                                                                                                          upgrade this code with 5x5 input and outputs boxes