Я пытался сделать это несколько раз, однако мне это не удалось. Я прикрепил свой код, который включает определенную пользователем функцию, которая просто находит последнюю строку в указанной области.
Цель этого состоит в том, чтобы вставить правильное количество необходимых строк (что правильно, но количество строк в обратном направлении), а затем заполнить эти новые строки информацией из другого листа в цикле. Информация находится путем перекрестной ссылки на номер активности, введенный в столбце A подзадач. Как только совпадение найдено, идеальной ситуацией будет копирование содержимого ячейки C слева от совпадения и вставка в новые вставленные столбцы. любая помощь будет оценена, так как это сводит меня с ума!
Sub createActivity()
Application.ScreenUpdating = False
Dim deliveryWs As Worksheet
Set deliveryWs = ActiveWorkbook.Worksheets("Delivery and acceptance sheet")
' Find start and end positions of activity table
activityStart = valuePos(deliveryWs, "A:A", "Activity")
activityEnd = valuePos(deliveryWs, "A:A", "Supplier Technical Focal point") - 1
' Insert row at the last position of Activity table
deliveryWs.Range("A" + CStr(activityEnd)).EntireRow.Insert
' Copy formatting from line above
Call copyFormattingAbove(deliveryWs, "A" + CStr(activityEnd))
' Create activity number
deliveryWs.Range("A" + CStr(activityEnd)) = deliveryWs.Range("A" + CStr(activityEnd - 1)) + 1
' Find start and end positions of deliverables table
deliverablesStart = valuePos(deliveryWs, "C:G", "Outputs / Deliverables")
deliverablesEnd = valuePos(deliveryWs, "A:G", "Tools / constraints")
' Insert row at the last position of Deliverables table
deliveryWs.Range("A" + CStr(deliverablesEnd)).EntireRow.Insert
' Copy formatting from line above
Call copyFormattingAbove(deliveryWs, "A" + CStr(deliverablesEnd))
' Numerate row according to activity
deliveryWs.Range("A" + CStr(deliverablesEnd)) = deliveryWs.Range("A" + CStr(activityEnd))
deliveryWs.Range("B" + CStr(deliverablesEnd)) = deliveryWs.Range("A" + CStr(activityEnd)) + 0.1
' Create new line for deliverable on Delivery and Validation for Invoicing table
Call updateInvoicingTable(deliveryWs, deliverablesEnd, deliverablesEnd - deliverablesStart)
' Create formula for activity Workload
deliveryWs.Range("L" + CStr(activityEnd)) = "=SUM(N" + CStr(deliverablesEnd) + ":N" + CStr(deliverablesEnd) + ")"
Application.ScreenUpdating = True
End Sub
Sub createDeliverable()
Application.ScreenUpdating = False
Dim activityNumber As Variant
Dim deliveryWs As Worksheet
Set deliveryWs = ActiveWorkbook.Worksheets("Delivery and acceptance sheet")
activityNumber = InputBox("Input Activity number")
If activityNumber = "" Then Exit Sub
'Count number of rows in column A with user specified number in (Activity Number)
' Find start and end positions of deliverables table
deliverablesStart = valuePos(deliveryWs, "C:G", "Outputs / Deliverables")
deliverablesEnd = valuePos(deliveryWs, "A:G", "Tools / constraints")
' Find start and end positions of activity within Deliverables table
delivActivStart = valuePos(deliveryWs, "A" + CStr(deliverablesStart) + ":A" + CStr(deliverablesEnd), "# " + CStr(activityNumber))
delivActivEnd = valuePos(deliveryWs, "A" + CStr(deliverablesStart) + ":A" + CStr(deliverablesEnd), "# " + CStr(activityNumber + 1))
If delivActivEnd = -1 Then
delivActivEnd = valuePos(deliveryWs, "A:G", "Tools / constraints")
End If
'Search through column in sub task sheet to identify matches with the activity number inputted
Dim iVal As Integer
Dim SubTaskWs As Worksheet
Set SubTaskWs = ActiveWorkbook.Worksheets("Sub tasks")
iVal = Application.WorksheetFunction.CountIf(SubTaskWs.Range("A:A"), activityNumber)
'Loop to identify number of rows and insert them inot spreadhseet in exisitng format
For i = 1 To (iVal - 1)
'Insert row at the last position of Activity table
deliveryWs.Range("A" + CStr(delivActivEnd)).EntireRow.Insert
' Copy formatting from line above
Call copyFormattingAbove(deliveryWs, "A" + CStr(delivActivEnd))
' Number Deliverable
deliveryNum = deliveryWs.Range("B" + CStr(delivActivEnd - 1)) + (0.1 * i)
deliveryWs.Range("B" + CStr(delivActivEnd)) = deliveryNum
' Update sum of workload for activity
Call updateActivityWorkload(deliveryWs, activityNumber, delivActivStart, delivActivEnd)
' Create new line for deliverable on Delivery and Validation for Invoicing table
Call updateInvoicingTable(deliveryWs, delivActivEnd, delivActivEnd - deliverablesStart)
Next i
End Sub
Private Function valuePos(ws, col, value)
Dim rng1 As Range
With ws.Range(col)
Set rng1 = .Find(value, LookIn:=xlValues, After:=.Cells(.Cells.Count), LookAt:=xlWhole)
End With
If rng1 Is Nothing Then
valuePos = -1
Else
valuePos = rng1.Row
End If
End Function
Private Sub copyFormattingAbove(ws, Cell)
ws.Range(Cell).Offset(-1, 0).EntireRow.Copy
ws.Range(Cell).Offset(0, 0).EntireRow.PasteSpecial xlPasteFormats
End Sub
Private Sub updateActivityWorkload(ws, activityNumber, delivActivStart, delivActivEnd)
' Find start and end positions of activity table
activityStart = valuePos(ws, "A:A", "Activity")
activityEnd = valuePos(ws, "A:A", "Supplier Technical Focal point") - 1
' Find activity row within Activity table
activityPos = valuePos(ws, "A" + CStr(activityStart) + ":A" + CStr(activityEnd), "# " + CStr(activityNumber))
' Update function
ws.Range("L" + CStr(activityPos)) = "=SUM(N" + CStr(delivActivStart) + ":O" + CStr(delivActivEnd) + ")"
End Sub
Private Sub updateInvoicingTable(ws, delivActivEnd, delivPos)
' Find start and end positions of invoicing table
invoicingStart = valuePos(ws, "A:D", "Outputs / Deliverables")
invoicingEnd = valuePos(ws, "A" + CStr(invoicingStart) + ":A" + CStr(300000), "") ' Will only work until row 300000
' Insert row for the new deliverable
ws.Range("A" + CStr(invoicingStart + delivPos)).EntireRow.Insert
' Copy formatting from line above
Call copyFormattingAbove(ws, "A" + CStr(invoicingStart + delivPos))
ws.Range("A" + CStr(invoicingStart + delivPos)) = "=$B" + CStr(delivActivEnd)
ws.Range("B" + CStr(invoicingStart + delivPos)) = "=$C" + CStr(delivActivEnd)
End Sub
введите описание изображения здесь