Ваша ошибка в том, что каждый раз, когда найдено совпадение, цикл For d =
... перезаписывает предыдущие результаты.
Быстрое и грязное исправление состоит в том, чтобы проверить строку результата на пустое, если найденопустой результат записи, затем выйдите из внутреннего цикла for.
Sub test()
Dim w_result As Worksheet
Dim w1 As Worksheet
Dim r As Long
Dim d As Long
Dim intLastRow As Long
Dim IntLastRow_Result As Long
Dim IntLastCol As Long
Dim arrID() As Variant
Dim arrName() As Variant
Dim arrNumber() As Variant
Dim ResultRow As Long
With ThisWorkbook
Set w1 = .Sheets("Sheet1")
Set w_result = .Sheets("Sheet2")
End With
With w1
intLastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
IntLastRow_Result = w_result.Cells(w_result.Rows.Count, 3).End(xlUp).Row '<~~ removed implicit active sheet reference
arrID = .Range(.Cells(5, 3), .Cells(intLastRow, 3))
arrName = .Range(.Cells(5, 1), .Cells(intLastRow, 1))
arrNumber = .Range(.Cells(5, 2), .Cells(intLastRow, 2))
For r = 1 To UBound(arrID, 1)
If Len(arrID(r, 1)) > 0 Then
For d = 4 To IntLastRow_Result
If w_result.Cells(d, 3) = arrID(r, 1) Then
If w_result.Cells(d, 5) = "Yes" Then
If IsEmpty(w_result.Cells(d, 6)) Then '<~~~ Added
w_result.Cells(d, 6) = arrName(r, 1)
w_result.Cells(d, 7) = arrNumber(r, 1)
Exit For '<~~~ Added
End If
End If
End If
Next
End If
Next r
End With
End Sub
Примечание: это очень неэффективное решение, но оно подойдет для небольших наборов данных.
Вот более эффективная версия, использующая Variant Array для результатов и обновляющая начальный индекс внутреннего цикла
Sub test()
Dim w_result As Worksheet
Dim w1 As Worksheet
Dim r As Long
Dim d As Long
Dim intLastRow As Long
Dim IntLastRow_Result As Long
Dim IntLastCol As Long
Dim arrID() As Variant
Dim arrName() As Variant
Dim arrNumber() As Variant
Dim Results() As Variant
Dim ResultStart As Long
Dim ResultRow As Long
With ThisWorkbook
Set w1 = .Sheets("Sheet1")
Set w_result = .Sheets("Sheet2")
End With
With w1
intLastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
IntLastRow_Result = w_result.Cells(w_result.Rows.Count, 3).End(xlUp).Row '<~~ removed implicit active sheet reference
Results = w_result.Cells(1, 1).Resize(IntLastRow_Result, 8).Value
w_result.Activate
arrID = .Range(.Cells(5, 3), .Cells(intLastRow, 3))
arrName = .Range(.Cells(5, 1), .Cells(intLastRow, 1))
arrNumber = .Range(.Cells(5, 2), .Cells(intLastRow, 2))
ResultStart = 4
For r = 1 To UBound(arrID, 1)
If Len(arrID(r, 1)) > 0 Then
For d = ResultStart To IntLastRow_Result
If Results(d, 3) = arrID(r, 1) Then
If Results(d, 5) = "Yes" Then
If IsEmpty(Results(d, 6)) Then '<~~~ Added
Results(d, 6) = arrName(r, 1)
Results(d, 7) = arrNumber(r, 1)
Exit For '<~~~ Added
End If
End If
ResultStart = ResultStart + 1
End If
Next
End If
Next r
End With
w_result.Cells(1, 1).Resize(IntLastRow_Result, 8).Value = Results
End Sub