Sub ToDo()
Dim WB As Workbook
Dim WS_Task As Worksheet
Dim WS_Action As Worksheet
Dim TaskRowIndex As Long
Dim ActionRowIndex As Long
Set WB = Excel.ActiveWorkbook
Set WS_Task = WB.Worksheets("Задачи")
Set WS_Action = WB.Worksheets("Действия")
For TaskRowIndex = WS_Task.Rows.Count To 2 Step -1
If WS_Task.Cells(TaskRowIndex, 1) <> "" Then
Exit For
End If
Next TaskRowIndex
For ActionRowIndex = WS_Action.Rows.Count To 2 Step -1
If WS_Action.Cells(ActionRowIndex, 1) <> "" Then
Exit For
End If
Next ActionRowIndex
For TaskRow = TaskRowIndex To 1 Step -1
TaskNumber = WS_Task.Cells(TaskRow, 1)
If TaskNumber <> "" And IsNumeric(TaskNumber) Then
TaskRowShift = TaskRow + 1
For ActionRow = ActionRowIndex To 2 Step -1
If WS_Action.Cells(ActionRow, 2) = TaskNumber Then
WS_Task.Rows(Int(TaskRowShift)).Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
WS_Task.Cells(TaskRowShift, 3) = WS_Action.Cells(ActionRow, 1)
WS_Task.Cells(TaskRowShift, 4) = WS_Action.Cells(ActionRow, 2)
WS_Task.Cells(TaskRowShift, 5) = WS_Action.Cells(ActionRow, 3)
End If
Next ActionRow
End If
Next TaskRow
End Sub
Лист "ЗАДАЧИ"
Лист "Действия"
Результат