Vba Can';t删除文本中包含特定关键字的行

Vba Can';t删除文本中包含特定关键字的行,vba,excel,Vba,Excel,我编写了一个宏来删除包含某些文本的行。如果任一关键字包含任何文本,宏将删除该行。但是,宏根本不起作用。也许,我做错了什么。希望有人能帮我纠正这一点。提前谢谢 以下是我正在尝试的: 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

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

以下是我正在尝试的:

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

的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
但是,您需要使用字符串函数,如
Instr
like
,以查看是否在较长的字符串中找到某个字符串

代码


的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
但是,您需要使用字符串函数,如
Instr
like
,以查看是否在较长的字符串中找到某个字符串

代码

像这样试试。
使用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
像这样试试。
使用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

我尽可能地制作我的代码示例如果您有任何问题,请询问

  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

我尽可能地制作我的代码示例如果您有任何问题,请询问

  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

谢谢Shai Rado的回答。宏部分工作。然而,有两件事它无法处理。1.不能触摸包含相同关键字且大小写混合的单元格。2.我把它用在500个细胞上。第一次运行时,它会删除20个,然后再次运行时,它会删除另外10个,依此类推,直到它们都包含关键字all@Shahin可以更具体一点,有什么不起作用?你有错误吗?在调试模式下运行时,lastRow的值是多少?请稍后回复。宏已正确标识lastRow。运行宏时没有抛出错误,也没有结果。谢谢Shai Rado的回答。宏部分工作。然而,有两件事它无法处理。1.不能触摸包含相同关键字且大小写混合的单元格。2.我把它用在500个细胞上。第一次运行时,它会删除20个,然后再次运行时,它会删除另外10个,依此类推,直到它们都包含关键字all@Shahin可以更具体一点,有什么不起作用?你有错误吗?在调试模式下运行时,lastRow的值是多少?请稍后回复。宏已正确标识lastRow。运行宏时没有抛出错误,也没有结果。谢谢李迪,它非常适合小写。上装还是混装呢?谢谢。对不起,李先生,我的无知。倒逗号内的文本必须用小写字母书写,如“mth”而不是我不懂的“mth”。谢谢李迪,它非常适合小写。上装还是混装呢?谢谢。对不起,李先生,我的无知。倒逗号内的文本必须用小写字母书写,如“mth”而不是我不理解的“mth”。