简体   繁体   English

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

[英]Insert row when cell above and below are different

How would you insert a row if, in the specified column the row above and below are different. 如果在指定的列的上方和下方的行不同,则如何插入行。 For example: 例如:
A1 : Hello
A2 : Hello
A3 : Goodbye

You would want to insert a row under row 2 and move 3 down. 您可能想在第2行下插入一行,然后向下移动3。

Thanks! 谢谢!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM