当上方和下方的单元格不同时插入行

时间:2013-08-22 21:36:36

标签: excel vba

如果在指定列中上下行不同,您将如何插入行?例如:
A1 : Hello
A2 : Hello
A3 : Goodbye

您可能希望在第2行下插入一行并向下移动3.

谢谢!

1 个答案:

答案 0 :(得分:1)

Sub insert()
    dim lastrow as long, i as long
    With ActiveSheet
        lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With
    For i = lastrow To 2 Step -1
        If Not Cells(i, 1) = Cells(i - 1, 1) Then
            Rows(i).insert shift:=xlShiftDown
        End If
    Next i
End Sub