简体   繁体   English

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

[英]VBA on how to delete rows that do not contain text

I have a column (H) that I need to search if it contains 1 of 4 words. 我有一列(H),如果包含4个单词中的1个,则需要搜索。 If it does not contain 1 of those 4 words I need the row to either be hidden or deleted. 如果它不包含这4个单词中的1个,我需要将该行隐藏或删除。 What is the best way to go about acheiving this? 实现这一目标的最佳方法是什么?

Try adding the following to a module and running. 尝试将以下内容添加到模块中并运行。 I have assumed column H has a header so the range begins on row 2. 我假设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

Or if you prefer to delete: 或者,如果您希望删除:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM