对此我不熟悉。
我想根据另一列中的值自动插入一定数量的行。可以这样做吗?
例如
Container Lifts
B0237 1
B0238 7
B0239 7
A8783 2
所以容器会被升降机数量分开
答案 0 :(得分:0)
嵌套在while循环中的一个for循环应该可以完成你的工作。为什么我们起诉而不是因为我们在现有数据之间添加新行时无法动态更改For循环的限制。你去吧:
Dim LastRow As Long
Dim RowAddNo As Long
LastRow = Range("A1").End(xlDown).Row
i = 2
While i <= LastRow
RowAddNo = Range("B" & i).Value
For J = 1 To RowAddNo
Rows(i + 1 & ":" & i + 1).Insert shift:=xlUp
LastRow = LastRow + 1
i = i + 1 'increasing the 'i' value to find the address for the next not empty row
Next
i = i + 1 'increasing the 'i' value to find the address for the next not empty row
Wend
PS:不要在没有i = i + 1的情况下尝试。它会进入无限循环:)
答案 1 :(得分:0)
这将为你做到:
Sub AddRows()
Dim X As Long
For X = Range("A" & Rows.Count).End(xlUp).Row To 2 Step -1 'Work backwards when inserting or deleting rows, so much easier than incrementing numbers
Range("A" & X).Offset(1, 0).Resize(Range("B" & X).Value, 1).EntireRow.Insert 'Insert the number of rows against the target row offset by 1 ie below it
Next
End Sub