如果两个条件匹配,则删除行

时间:2019-03-29 09:39:47

标签: excel vba

我有两个包含数据的工作表,并且如果两个条件匹配,我希望在sheet1中删除行。我已经确定了一个想法,因此更容易理解。

enter image description here

我已经完成了代码的第一部分,即在一个条件匹配时删除行,但是它也删除了我想保留的空白行。

Private Sub CommandButton1_Click()

Application.ScreenUpdating = False

Dim iListCount As Long
Dim x As Variant
Dim iCtr As Long

iListCount = Sheets("Sheet1").Cells(Rows.Count, "N").End(xlUp).Row

For Each x In Sheets("Laoseis").Range("B4:B" & Sheets("Sheet1").Cells(Rows.Count, "N").End(xlUp).Row)
  For iCtr = iListCount To 16 Step -1
    If x.Value = Sheets("Sheet1").Cells(iCtr, 14).Value Then
      Sheets("Sheet1").Cells(iCtr, 14).EntireRow.Delete
    End If
  Next iCtr
Next
Application.ScreenUpdating = True

End Sub

1 个答案:

答案 0 :(得分:0)

我认为您可以使用以下内容:

Option Explicit

Private Sub CommandButton1_Click()

    Dim iListCount As Long, iCtr As Long
    Dim cell As Range

    Application.ScreenUpdating = False

    With ThisWorkbook

        iListCount = .Sheets("Sheet1").Cells(Rows.Count, "N").End(xlUp).Row

        For Each cell In .Sheets("Laoseis").Range("B4:B" & iListCount)

          For iCtr = iListCount To 16 Step -1

            If cell.Value = .Sheets("Sheet1").Cells(iCtr, 14).Value And cell.Value <> "" Then
                .Sheets("Sheet1").Cells(iCtr, 14).EntireRow.Delete
            End If

          Next iCtr

        Next cell

    End With

    Application.ScreenUpdating = True

End Sub