macros每隔一行添加行

我有我的Excel电子表格上运行以下macros:

Sub yTest01() For i = 10 To 72 Step 2 Cells(i, 1).EntireRow.Insert Next i End Sub 

它每隔一行添加一个新行。 但是,我想每个添加五行。 我尝试了以下,但它不起作用

 Sub yTest01() For i = 10 To 72 Step 2 Cells(i, 1).EntireRow.Insert Cells(i, 1).EntireRow.Insert Cells(i, 1).EntireRow.Insert Cells(i, 1).EntireRow.Insert Cells(i, 1).EntireRow.Insert Next i End Sub 

任何想法我可以做什么?

我的猜测是,你想要一行现有的数据,然后5行空白。 尝试以下

 Sub yTest01() For i = 72 To 10 Step -1 Cells(i, 1).EntireRow.Insert Cells(i, 1).EntireRow.Insert Cells(i, 1).EntireRow.Insert Cells(i, 1).EntireRow.Insert Cells(i, 1).EntireRow.Insert Next i End Sub 

如果你想要的是两行数据然后五个空行然后改变步骤-2

当增加或减less行时,最好的做法是向后循环。

作为最佳实践,我努力减lessVBA必须跨越边界与Excel通信的次数。

因此,不要为每个插入在边界上进行五次呼叫,而是最好制作一个。 向后退步可能会有所帮助,但并不是必需的。

 For i = 0 To 62 * 6 Step 6 [10:10].Resize(5).Offset(i).Insert Next 

注意:0和62的迭代值仅仅是OP的行号调整为从零开始。

为什么它不工作,为什么使用第二步? 移动行后,你可以移动我的5个办法吗?

 Sub yTest01() Dim i As Integer For i = 10 To 72 Step 1 Cells(i, 1).EntireRow.Insert Cells(i, 1).EntireRow.Insert Cells(i, 1).EntireRow.Insert Cells(i, 1).EntireRow.Insert Cells(i, 1).EntireRow.Insert i = i + 5 Next i End Sub