我是VBA&希望有人可以提供帮助 - 我搜索了这个具体问题,但我无法找到我正在寻找的确切内容。
我正在尝试编写一个模块(使用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
我现在要做的是创建一个循环,它将为整个工作表执行此操作。这就是我现在所拥有的:
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.
非常感谢任何有关此问题的帮助,资源和建议。
提前致谢!
最佳, 利兹
答案 0 :(得分:0)
从代码中删除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