Delete two rows at the same time by using VBA in a given condition

The condition is that in Column A, if the cell (A1,A2,A3....) is a merged cell, then delete the entire row and the row above this cell

I know how to delete one entire row but I duplicate the language when trying to delete the 2nd row (can I put them together?)

Sub test()

Dim i As Integer

For i = 1 To 300 Step 1
    If Cells(i, 1).MergeCells = True Then
        Rows(i - 1).EntireRow.Delete 'delete  the row above the merged cell
    End If
Next i

For i = 1 To 300 Step 1
    If Cells(i, 1).MergeCells = True Then
    Rows(i).EntireRow.Delete 'delete another row where the merged cell is
    End If
Next i

End Sub

2 answers

  • answered 2018-05-16 06:11 DisplayName

    you could try this:

    Sub test()
        Dim i As Long
    
        For i = 300 To 1 Step -1
            If Cells(i + 1, 1).MergeCells Then Rows(i).Resize(2).EntireRow.Delete 'delete the row with the merged cell and the row above
        Next
        If Cells(1, 1).MergeCells Then Rows(1).EntireRow.Delete 'if first cell is merged, then delete its row
    End Sub
    

  • answered 2018-05-16 06:11 Lee Li Fong

    For i = 1 To 300 Step 1
        If Cells(i, 1).MergeCells = True Then
            Cells(i, 1).MergeArea.EntireRow.Delete
        End If
    Next