插入4行

时间:2014-04-01 21:18:05

标签: excel vba excel-vba

我正在尝试在单元格列4没有相同值时插入一行。由于某种原因,它插入4行。它只发生在一开始。可能有什么不对?

感谢您的帮助!

If Cells(j, 4) <> Cells(j - 1, 4) Then
        Cells(j, 1).EntireRow.Insert
        j = j + 1
    End If

1 个答案:

答案 0 :(得分:0)

这是你在尝试的吗?

Sub Sample()
    Dim ws As Worksheet
    Dim lRow As Long, i As Long

    '~~> Change this to the relevant sheet
    Set ws = ThisWorkbook.Sheets("Sheet1")

    With ws
        lRow = .Range("D" & .Rows.Count).End(xlUp).Row

        For i = lRow To 2 Step -1
            If .Cells(i, 4) <> Cells(i - 1, 4) Then .Cells(i, 1).EntireRow.Insert
        Next i
    End With
End Sub

<强>截图:

enter image description here