如何在VBA中使用“InsertRow”以获取单元格范围(Excel 2013)
我是VBA的新手,希望有人可以提供帮助 – 我search了这个特定的问题,但是我一直无法find我正在寻找的东西。
我正在尝试编写一个模块(使用MS Office Professional Excel 2013),该模块将基于每行的“O”列中的整数插入行。
我可以通过对单元进行硬编码来获得这一行的工作:
Sub InsertRow() Set currentcell = Sheets("Sheet1").Range("O2") Dim i As Integer, n As Integer, m As Long n = currentcell.Value m = currentcell.Row For i = 1 To n Rows(m + 1 * i).Insert Next i End Sub
我现在想要做的是创build一个循环,将执行整个工作表的这一行动。 这是我现在所拥有的:
Sub InsertRow() Set currentcell = Sheets("Sheet1").Range("O2:O400") Dim i As Integer, n As Integer, m As Long n = currentcell.Value m = currentcell.Row For i = 1 To n Rows(m + 1 * i).Insert Next i Loop End Sub
这当前返回Compile Error: Loop Without Do.
任何帮助,资源,这个问题的build议将不胜感激。
提前致谢!
最好的Liz
从您的代码中删除loop
。 这是Do
循环结束的语法,而不是for
循环
Sub InsertRow() Set currentcell = Sheets("Sheet1").Range("O2:O400") Dim i As Integer, n As Integer, m As Long n = currentcell.Value m = currentcell.Row For i = 1 To n Rows(m + 1 * i).Insert Next i End Sub
更新:在评论后显示循环范围
Sub InsertRow() Dim rng as Range Dim i as integer, n as integer, m as integer Set rng = Sheets("Sheet1").Range("O2:0400") For Each c in rng i = c.value m = c.row Rows(m + 1 * i).Insert Next c