根据多列中的字符串条件删除行



我有下面的代码,它搜索D列中的每个单元格,并删除包含字符串";文本1";。我现在需要扩展删除的标准,以查找字符串";文本2";,但是在列C中。我能用这个代码做什么,这样它只删除字符串为"的行;文本1";在D列中;文本2";在C列中?

Dim Cell As Range, cRange As Range, LastRow As Long, x As Long
' Defines LastRow as the last row of data based on column D
LastRow = Sheets(Sheet1).Cells(Rows.Count, "D").End(xlUp).Row
' Sets check range as D2 to the last row of D
Set cRange = Range("D2:D" & LastRow)
' For each cell in the check range, working from the bottom upwards
For x = cRange.Cells.Count To 1 Step -1
With cRange.Cells(x)
' If the cell contains "Text1", delete entire row
If InStr(.Value, "Text1") > 0 Then
.EntireRow.Delete
End If
End With
' Check next cell, working upwards
Next x

相当粗糙,但更改此

If InStr(.Value, "Text1") > 0 Then

到这个

If InStr(.Value, "Text1") > 0 And InStr(.Offset(0, -1).Value, "Text 2") > 0 Then

尽管我在打电话,但还是试一下。

相关内容

  • 没有找到相关文章

最新更新