[英]Insert Multiple Rows under Active Cell using VBA in Excel
我正在編寫一個遍歷每一行的宏(某些行包含的行比其他行多),並對數列進行計數,並在所選行的下方插入與空白單元格相同的數字。 目標是最終將數據復制並粘貼到單個列中。
我可以插入正確數量的空白單元格,但是它們會插入到所選行的上方。 我希望將它們插入所選行的下方。 任何幫助,將不勝感激!
rowCount = ActiveSheet.Cells(rows.Count, 5).End(xlUp).Row
ActiveSheet.Range("E1:E" & rowCount).Select
For c = 1 To rowCount
columnCount = ActiveSheet.Cells(c,
Columns.Count).End(xlToLeft).Column - 5
If columnCount > 0 Then
Let CopyRange = "E" & c & ":" & "E" & (c + columnCount - 1)
ActiveSheet.Range(CopyRange).EntireRow.Insert Shift:=xlDown
End If
rowCount = ActiveSheet.Cells(rows.Count, 5).End(xlUp).Row
c = c + columnCount
Next c
在您當前要插入的行下方插入1行。 另外,您可以通過向后循環來簡化此過程。
rowCount = ActiveSheet.Cells(rows.Count, 5).End(xlUp).Row
For c = rowCount to 1 step -1
columnCount = ActiveSheet.Cells(c, Columns.Count).End(xlToLeft).Column - 5
If columnCount > 0 Then
Set CopyRange = "E" & c + 1 & ":" & "E" & (c + columnCount)
ActiveSheet.Range(CopyRange).EntireRow.Insert Shift:=xlDown
End If
Next c
注意:這未經測試
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.