VBA关于如何删除不包含文本的行

时间:2015-07-09 22:03:10

标签: excel-vba contains delete-row vba excel

我有一个列(H),我需要搜索它是否包含4个单词中的1个。如果它不包含这4个单词中的1个,我需要隐藏或删除该行。实现这个目标的最佳方法是什么?

2 个答案:

答案 0 :(得分:1)

尝试将以下内容添加到模块并运行。我假设列H有一个标题,所以范围从第2行开始。

Public Sub Test()
Dim rng As Range
Dim row As Range
Dim cell As Range

Set rng = Range("H2:H7")

For Each row In rng.Rows
For Each cell In row.Cells

Select Case cell.Value
Case "Red", "Blue", "Green", "White"
'Do nothing
Case Else
row.Hidden = True
End Select

Next cell
Next row
End Sub

答案 1 :(得分:0)

或者如果您想删除:

lastRow = Range("H65000").End(xlUp).Row
For i = lastRow To 2 Step -1
Select Case Cells(i, 8).Value
    Case "Red", "Blue", "Green", "White"
        'Do nothing
    Case Else
        Cells(i, 8).EntireRow.Delete
End Select
Next i