如何编写一个在每个填充行之间插入n个空行的宏? (其中n为2或更大)

时间:2017-03-04 08:45:34

标签: excel vba excel-vba

(a)为什么我的代码在填充的行之间插入3行而不是只插入1? (b)如何修改代码以仅插入1行,或者在填充的行之间插入5行?

在运行代码之前;绿色轮廓显示我的选择

enter image description here

运行代码后

enter image description here

代码

Public Sub Insertrows()

Dim MyRange As Range
Dim MyCell As Range

Set MyRange = Selection

For Each MyCell In MyRange
    If Not IsEmpty(MyCell) Then
        MyCell.Offset(1, 0).EntireRow.Insert
    End If
Next MyCell

End Sub

1 个答案:

答案 0 :(得分:2)

因为您正在遍历所选范围的所有单元格,所以:A1,A2,...,A10然后是B1,B2,... B10,并且您在任何单元格中不会多次点击空单元格行

使用

For Each MyCell In MyRange.Columns(1).Cells
相关问题