2017-08-15 101 views
0

我写了一个宏来删除包含其中某些文本的行。如果任一关键字包含任何文本,宏将删除该行。但是,这个宏根本不起作用。也许,我做了错误的事情。希望有人能帮我纠正这一点。提前致谢。无法删除文本中包含某些关键字的行

以下是我与努力:

Sub customized_row_removal() 
    Dim i As Long 
    i = 2 
    Do Until Cells(i, 1).Value = "" 
     If Cells(i, 1).Value = "mth" Or "rtd" Or "npt" Then 
      Cells(i, 1).Select 
      Selection.EntireRow.Delete 
     End If 
     i = i + 1 
    Loop 
End Sub 

我正在寻找在删除文本中的关键字:

AIRLINE DRIVE OWNER mth 
A rtd REPAIRS INC 
AANA MICHAEL B ET AL 
ABASS OLADOKUN 
ABBOTT npt P 
AIRLINE AANA MTH 
ABASS REPAIRS NPT 

回答

1

尝试这样的。
使用Lcase怎么样?

Sub customized_row_removal() 
    Dim rngDB As Range, rngU As Range, rng As Range 
    Dim Ws As Worksheet 

    Set Ws = Sheets(1) 
    With Ws 
     Set rngDB = .Range("a2", .Range("a" & Rows.Count)) 
    End With 

    For Each rng In rngDB 
     If InStr(LCase(rng), "mth") Or InStr(LCase(rng), "rtd") Or InStr(LCase(rng), "npt") Then 
      If rngU Is Nothing Then 
       Set rngU = rng 
      Else 
       Set rngU = Union(rngU, rng) 
      End If 
     End If 
    Next rng 
    If rngU Is Nothing Then 
    Else 
     rngU.EntireRow.Delete 
    End If 
End Sub 
+0

谢谢Dy.Lee,它适用于小写。怎么样上层或混合的情况?谢谢。 – SIM

+0

@Shahin,它在任何情况下工作 –

+0

对不起Dy.Lee,因为我的无知。引号内的文字必须用小写字母表示,如我不明白的“mth”而不是“MTH”。 – SIM

1
Or

VBA语法是错误的,

If Cells(i, 1).Value = "mth" Or "rtd" Or "npt" Then 

应该是:

If Cells(i, 1).Value = "mth" Or Cells(i, 1).Value = "rtd" Or Cells(i, 1).Value = "npt" Then 

但是,你需要使用一个字符串函数,如InstrLike,看是否有特定的字符串是一个较长的字符串内找到。

代码

Option Explicit 

Sub customized_row_removal() 

Dim WordsArr As Variant 
Dim WordsEl As Variant 
Dim i As Long, LastRow As Long 
Dim Sht As Worksheet 

WordsArr = Array("mth", "rtd", "npt") 

Set Sht = Worksheets("Sheet1") 
With Sht 
    ' get last row in column "A" 
    LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row 
    For i = LastRow To 2 Step -1 
     For Each WordsEl In WordsArr 
      If LCase(.Cells(i, 1).Value) Like "*" & WordsEl & "*" Then 
       .Rows(i).Delete 
      End If 
     Next WordsEl 
    Next i 
End With 

End Sub  
+0

感谢夏嘉曦瑞士雷达表,你的答案。你的宏部分工作。然而,它不能处理两件大事。 1.不能触摸含有大小写混合或大写的相同关键字的单元格。我将它应用于500个细胞。第一次运行时,它删除20,然后再次运行时删除另一个10等,直到它们都包含关键字。 – SIM

+0

@Shahin尝试编辑的代码 –

+0

我刚刚尝试,但纠正的代码根本不起作用 – SIM

0

我尽量让我的代码示例,我可以,如果你有任何问题,请咨询

Private Sub remove_word_raw() 
'PURPOSE: Clear out all cells that contain a specific word/phrase 

Dim Rng As Range 
Dim cell As Range 
Dim ContainWord As String 

'What range do you want to search? 

    Set Rng = Range("A2:A25") 

    'sub for the word 

    shorttext1 = "mth" 
    shorttext2 = "rtd" 
    shorttext3 = "npt" 
'What phrase do you want to test for? 

    ContainWord1 = shorttext1 
    ContainWord2 = shorttext2 
    ContainWord3 = shorttext3 

'Loop through each cell in range and test cell contents 

    For Each cell In Rng.Cells 
    If cell.Value2 = ContainWord1 Then cell.EntireRow.Delete 

    Next 
    For Each cell In Rng.Cells 
    If cell.Value2 = ContainWord2 Then cell.EntireRow.Delete 

    Next 
    For Each cell In Rng.Cells 

     If cell.Value2 = ContainWord3 Then cell.EntireRow.Delete 
     Next cell 
End Sub 
相关问题