【问题标题】:VBA Delete Row contains specific exact wordVBA删除行包含特定的确切词
【发布时间】:2021-08-06 17:57:51
【问题描述】:

如果 A 列包含“string1”,我想从 Excel 中删除行,如下表所示:

A1 B1
string1 string2 string3 string4
string5 string6 string7 string8

我正在使用以下代码:

 Sub DeleteRows()

    Dim rng As Range
    Dim pos As Integer
    Set rng = ActiveSheet.UsedRange
    
    For i = rng.Cells.Count To 1 Step -1
        pos = InStr(LCase(rng.Item(i).Value), LCase("string1"))
        If pos > 0 Then
            rng.Item(i).EntireRow.Delete
        End If
    Next i
End Sub

问题在于,如果 A 列包含“string11”而不是“string1”,它也会删除该行。 请问有什么建议吗? 谢谢

【问题讨论】:

  • if LCase(rng(i).text) = "string1" then
  • 但最好使用.Value,而不是.Text
  • @BigBen 为什么.Value 更好?
  • stackoverflow.com/questions/17359835/… 好吧,这支持.Value2,但无论如何,第一段解释了.Text 的潜在陷阱。如果 OP 的数据都是字符串,可能并不重要,但我想我倾向于避免 .Text
  • 尝试使用Split()或RegExp

标签: excel vba vba7


【解决方案1】:

使用Split()(已测试;未优化):

Sub DeleteRows()
    Dim rng As Range
    Set rng = ActiveSheet.UsedRange
    
    For i = rng.Cells.Count To 1 Step -1
        For Each w In Split(LCase(rng(i).Value))
            If w = "string1" Then
                rng(i).EntireRow.Delete
                Exit For
            End If
        Next
    Next i
End Sub

编辑 2

Sub DeleteRows()
    Dim rng As Range, i As Long, w, arr
    arr = Array("string1", "string2", "string3")
    Set rng = ActiveSheet.UsedRange
    
    For i = rng.Cells.Count To 1 Step -1
        For Each w In Split(LCase(rng(i).Value))
            If IsNumeric(Application.Match(w, arr, 0)) Then
                rng(i).EntireRow.Delete
                Exit For
            End If
        Next
    Next i
End Sub

【讨论】:

  • 非常感谢@АлексейР.. 这真的很有帮助.. 我们可以在字符串列表中搜索而不是“=”,例如: If w in ("string1, "string2", " string3")?
  • 你可以使用 Application.Match(w,array(),0)
  • @ АлексейР.. 感谢您的回答,但我对 VBA 有点陌生.. 请问您上面的代码怎么写?
  • 使用Application.Match()查看Edit2
【解决方案2】:

尝试将“在字符串中找到”与“字符串长度相同”结合起来。像这样:

Sub DeleteRows()

   Dim rng As Range
   Set rng = ActiveSheet.UsedRange
   
   For i = rng.Cells.Count To 1 Step -1
       pos = InStr(LCase(rng.Item(i).Value), LCase("delete"))
       If pos > 0 And (Len(rng.Item(i).Value) = Len(Lcase("delete"))) Then
           rng.Item(i).EntireRow.Delete
       End If
   Next i
End Sub

或者,如果您想比较整个单元格内容,您可以将其简化为:

Sub DeleteRows()

   Dim rng As Range
   Dim pos As Integer
   Set rng = ActiveSheet.UsedRange
   
   For i = rng.Cells.Count To 1 Step -1
       If LCase(rng.Item(i).Value) = LCase("delete") Then rng.Item(i).EntireRow.Delete
   Next i
End Sub

【讨论】:

  • 为什么不简单地使用=StrComp 而不是InStr
  • 因为原始解决方案(保留)是在对 OP 的原始代码进行最小更改的情况下创建的。现在还进行了编辑以添加一个更简单的解决方案,如果 OP 想要比较整个单元格内容而不是一个单词。
【解决方案3】:

你不需要pos变量,只需比较If语句中的值!

Sub DeleteRows()

    Dim rng As Range
    Dim pos As Integer
    Set rng = ActiveSheet.UsedRange
    
    For i = rng.Cells.Count To 1 Step -1
        If rng.Item(i).Value = "delete" Then
            rng.Item(i).EntireRow.Delete
        End If
    Next i
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-31
    • 2021-03-19
    • 2021-06-20
    • 1970-01-01
    • 2019-05-18
    • 2023-03-24
    相关资源
    最近更新 更多