深く考えてませんが、これでどうにかなると思います。
Sub Test()
Dim c As Range, Fr As Range
Dim i As Long, mCount As Integer, SumCount As Integer
Dim oRow1 As Long, oRow2 As Long, oColumn1 As Long, oColumn2 As Long
Dim sData As Variant
SumCount = 0
sData = Range("K10").Value
For i = 6 To 18 Step 3
For Each c In Range(Cells(i, "D"), Cells(i + 2, "G"))
If c.Value = sData Then
oRow1 = -1: oRow2 = 1
oColumn1 = -1: oColumn2 = 1
If c.Row = i Then
oRow1 = 0
ElseIf c.Row = i + 2 Then
oRow2 = 0
End If
If c.Column = Range("D:D").Column Then
oColumn1 = 0
ElseIf c.Column = Range("G:G").Column Then
oColumn2 = 0
End If
mCount = mSearch(c, oRow1, oColumn1, oRow2, oColumn2)
If mCount = 1 Then
SumCount = SumCount + mCount
Exit For
End If
End If
Next
Next
Set Fr = Range("AA10:AJ10").Find(What:=sData, LookIn:=xlValues, lookat:=xlWhole)
If Not Fr Is Nothing Then
Cells(Rows.Count, Fr.Column).End(xlUp).Offset(1, 0).Value = SumCount
Else
MsgBox sData & "が見つかりません"
End If
If SumCount = 2 Then
Call mSet(Range("S:S").Column, sData)
ElseIf SumCount > 2 Then
Call mSet(Range("M:M").Column, sData)
End If
End Sub
Function mSet(ByVal mColumn As Long, ByVal mData As Variant)
Dim LastRow As Long
Dim mCount As Integer
LastRow = Cells(Rows.Count, mColumn).End(xlUp).Row
If LastRow = 10 Then
LastRow = 11
End If
mCount = WorksheetFunction.Count(Range(Cells(LastRow, mColumn), Cells(LastRow, mColumn).Offset(0, 5)))
If mCount = 6 Then
Cells(LastRow + 1, mColumn).Value = mData
Else
Cells(LastRow, mColumn).Offset(0, mCount).Value = mData
End If
End Function
Function mSearch(ByRef d As Range, ByVal oRow1 As Long, ByVal oColumn1 As Long, ByVal oRow2 As Long, ByVal oColumn2 As Long) As Integer
Dim c As Range
For Each c In Range(d.Offset(oRow1, oColumn1), d.Offset(oRow2, oColumn2))
If c.Address <> d.Address Then
If c.Value = d.Value Then
mSearch = 1
Exit Function
End If
End If
Next
mSearch = 0
End Function
お礼
そうなんですよw 下の方をカウントしてるみたいで、目的の所とは違う場所をカウントしてるようです。 今頑張ってソースをみなおしてます。(D6 : G20 )をカウントのフォーカスに定めるには何処から見直しが必要ですか?