Пожалуйста, проверьте следующий код. Возвращается, начиная с диапазона «K4: M4»:
Sub matchdates()
Dim sh As Worksheet, lastRA As Long, lastRE As Long, i As Long, j As Long
Dim arrA As Variant, arrE As Variant, arrRez As Variant, k As Long
Set sh = sheet2 'use here your real sheet
lastRA = sh.Range("A" & sh.Rows.count).End(xlUp).Row
lastRE = sh.Range("E" & sh.Rows.count).End(xlUp).Row
arrA = sh.Range("A4:B" & lastRA).Value 'input the range in an array
arrE = sh.Range("E4:F" & lastRE).Value 'input the range in an array
ReDim arrRez(1 To UBound(arrA, 1), 1 To 3) ' set dimension of the array
' collecting the resultr
k = 1 'initialize the first arrRez row number
For i = 1 To UBound(arrA, 1)
For j = 1 To UBound(arrE, 1)
If DateValue(arrA(i, 1)) = DateValue(arrE(j, 1)) Then 'when a match is found (independent of sorting):
'the array collecting the result is loaded with the appropriate mathching data:
arrRez(k, 1) = arrA(i, 1): arrRez(k, 2) = arrA(i, 2): arrRez(k, 3) = arrE(j, 2)
k = k + 1 'the array row number is incremented
Exit For 'exiting the loop in order to save time
End If
Next j
Next i
If arrRez(1, 1) <> Empty Then
'the array keeping the matching result drops its data:
With sh.Range("K4:M" & k + 2)
.Value = arrRez
.EntireColumn.AutoFit 'a little format to column autofit
End With
End If
End Sub