在给定条件下使用VBA同时删除两行

问题描述 投票:0回答:2

条件是在Column A中,如果单元格(A1,A2,A3....)是合并的单元格,则删除整个行和此单元格上方的行

我知道如何删除整行但是在尝试删除第二行时我会复制语言(我可以将它们放在一起吗?)

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
excel vba excel-vba
2个回答
2
投票

你可以试试这个:

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

2
投票
For i = 1 To 300 Step 1
    If Cells(i, 1).MergeCells = True Then
        Cells(i, 1).MergeArea.EntireRow.Delete
    End If
Next
© www.soinside.com 2019 - 2024. All rights reserved.