当下面的单元格值不等于活动单元格值时如何插入新行

当下面的单元格值不等于活动单元格值时如何插入新行

只是如何获得此结果:

enter image description here

目前,vba代码为:

Sub InsertBlankRowsBasedOnCellValue()

Dim Col As Variant
Dim BlankRows As Long
Dim LastRow As Long
Dim R As Long
Dim StartRow As Long

    Col = "A"
    StartRow = 1
    BlankRows = 1

        LastRow = Cells(Rows.Count, Col).End(xlUp).Row

        Application.ScreenUpdating = False

        With ActiveSheet
For R = LastRow To StartRow + 1 Step -1
If .Cells(R, Col) <> LastRow Then
.Cells(R, Col).EntireRow.Insert Shift:=xlDown
End If
Next R
End With
Application.ScreenUpdating = True

End Sub
评论
少黑
少黑

您应该添加另一个实际进行比较的条件:

For R = LastRow To StartRow + 1 Step -1
    If R <> LastRow Then
        If .Cells(R, Col).Value <> .Cells(R + 1, Col).Value Then
            .Cells(R + 1, Col).EntireRow.Insert Shift:=xlDown
        End If
    End If
Next R
点赞
评论