[英]Excel VBA to insert rows based on cell value and copy columns down based on that number
I know there are several questions and answers about using Excel VBA to copy and insert rows based on a cell value but I have an additional requirement that makes it difficult to find a solution. 我知道有几个问题和答案关于使用Excel VBA复制和插入基于单元格值的行,但我有一个额外的要求,使得很难找到解决方案。 I'm stuck at this point and need help. 我陷入困境,需要帮助。
I have a spreadsheet along the lines of the following: 我有一个电子表格,如下所示:
Name Unit Count Req1 Req2 Req3 Req4 ... ... Req25
Apple 304 5 Apple1 Apple2 Apple3 Apple4 ... Apple5
Pear 562 2 Pear1 Pear2
Kiwi 471 4 Kiwi1 Kiwi2 Kiwi3 Kiwi4
The spreadsheet has columns for "Req1" through "Req25." 电子表格包含“Req1”到“Req25”的列。 If the"count" is 5, then "Req1" through "Req5" columns will have data. 如果“count”为5,则“Req1”到“Req5”列将包含数据。 The "count" will vary per row leaving the reminder of the columns to "Req25" blank. “计数”每行会有所不同,将“Req25”列的提醒留空。 I need to insert rows based on "count" -1, copy down all columns up to "count" column, and then move "Req2," "Req3," etc. down to the corresponding inserted row in the "Req1" column. 我需要根据“count”-1插入行,将所有列复制到“count”列,然后将“Req2”,“Req3”等向下移动到“Req1”列中相应的插入行。 I'm probably not explaining it very well. 我可能不会很好地解释它。
I need the end result to look like: 我需要最终结果看起来像:
Name Unit Count Req1
Apple 304 5 Apple1
Apple 304 5 Apple2
Apple 304 5 Apple3
Apple 304 5 Apple4
Apple 304 5 Apple5
Pear 562 2 Pear1
Pear 562 2 Pear2
Kiwi 471 4 Kiwi1
Kiwi 471 4 Kiwi2
Kiwi 471 4 Kiwi3
Kiwi 471 4 Kiwi4
I am able to insert the correct number of rows but I'm stuck on looping through the columns and moving them down into the "Req1" column. 我能够插入正确数量的行,但我仍然坚持循环遍历列并将它们向下移动到“Req1”列。
Any help is GREATLY appreciated!! 任何帮助是极大的赞赏!! Thanks in advance! 提前致谢!
This macro will do what you want but instead of inserting rows it will put the data into a new sheet; 这个宏可以做你想要的,但不是插入行,而是将数据放入一个新的表中; You just need to add a sheet for output and change the name for input and output sheets in the code. 您只需要为输出添加工作表,并在代码中更改输入和输出工作表的名称。
Dim mOut As Worksheet
Dim mInp As Worksheet
Dim num As Integer
Dim i As Integer
Dim j As Integer
Dim c As Integer
Sub Copy()
Set mInp = Worksheets("Your Sheet Name")
Set mOut = Worksheets("Create Another Sheet for Output")
mOut.Cells(1, 1) = mInp.Cells(1, 1)
mOut.Cells(1, 2) = mInp.Cells(1, 2)
mOut.Cells(1, 3) = mInp.Cells(1, 3)
mOut.Cells(1, 4) = "Req"
i = 2
num = 2
While mInp.Cells(i, 1) <> ""
c = mInp.Cells(i, 3)
For j = 1 To c
mOut.Cells(num, 1) = mInp.Cells(i, 1)
mOut.Cells(num, 2) = mInp.Cells(i, 2)
mOut.Cells(num, 3) = mInp.Cells(i, 3)
mOut.Cells(num, 4) = mInp.Cells(i, j + 3)
num = num + 1
Next j
i = i + 1
Wend
End Sub
If you want to pursue the solution that you had by inserting the rows you need to add this loop after you inserted them. 如果您想通过插入行来寻求所需的解决方案,则需要在插入后添加此循环。 Also, you need to count number of rows while adding them. 此外,您还需要在添加行数时计算行数。 I do not have your code to see how it can be done but am sure it is easy to do so. 我没有你的代码来看看它是如何完成的,但我相信这很容易。
For i = 2 To NumRows 'Number of rows (Sum of the inserted and original rows)
If mInp.Cells(i, 1) <> "" Then
irow = i
Count = 1
Else
mInp.Cells(i, 1) = mInp.Cells(irow, 1)
mInp.Cells(i, 2) = mInp.Cells(irow, 2)
mInp.Cells(i, 3) = mInp.Cells(irow, 3)
mInp.Cells(i, 4) = mInp.Cells(irow, 4 + Count)
Count = Count + 1
End If
Next i
you could work with arrays and slice them with Application.Index()
您可以使用数组并使用Application.Index()
它们进行切片
Sub main()
Dim data1 As Variant, data2 As Variant
Dim i As Long
With Range("A2", Cells(Rows.Count, "A").End(xlUp))
data1 = .Resize(, 3).Value
data2 = .Offset(, 3).Resize(, 25).Value
.Resize(, 28).ClearContents
End With
For i = LBound(data1) To UBound(data1)
With Cells(Rows.Count, 1).End(xlUp).Offset(1)
.Resize(data1(i, 3), 3) = Application.Index(data1, i, 0)
.Offset(, 3).Resize(data1(i, 3), 1) = Application.Transpose(Application.Index(data2, i, 0))
End With
Next
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.