【问题标题】:VBA: How to use like operator to a list of value?VBA:如何对值列表使用like运算符?
【发布时间】:2019-08-04 07:13:06
【问题描述】:

这是我的代码的一部分。有没有办法让它变得简单?谢谢。

For i = 2 To ws.Range("E1").CurrentRegion.Rows.Count

If ws.Cells(i, 4).Value Like ("*SSI*") Then ws.Cells(i, 4).EntireRow.Delete
If ws.Cells(i, 4).Value Like ("*Settlement instruction*") Then ws.Cells(i, 4).EntireRow.Delete
If ws.Cells(i, 4).Value Like ("*delivery Instruction*") Then ws.Cells(i, 4).EntireRow.Delete
If ws.Cells(i, 4).Value Like ("*Request form*") Then ws.Cells(i, 4).EntireRow.Delete
If ws.cells(i, 4).Value Like ("*Sales to onboarding*") Then ws.Cells(i, 4).EntireRow.Delete
If ws.Cells(i, 4).Value Like ("*Application*") Then ws.Cells(i, 4).EntireRow.Delete
If ws.Cells(i, 4).Value Like ("*Doc Check list*") Then ws.Cells(i, 4).EntireRow.Delete
If ws.Cells(i, 4).Value Like ("*Prime to Credit*") Then ws.Cells(i, 4).EntireRow.Delete
If ws.Cells(i, 4).Value Like ("*Prime to Legal*") Then ws.Cells(i, 4).EntireRow.Delete
If ws.Cells(i, 4).Value Like ("*Prime_Legal*") Then ws.Cells(i, 4).EntireRow.Delete
If ws.Cells(i, 4).Value Like ("*Prime_Credit*") Then ws.Cells(i, 4).EntireRow.Delete
If ws.Cells(i, 4).Value Like ("*LEXIS*") Then ws.Cells(i, 4).EntireRow.Delete
If ws.Cells(i, 4).Value Like ("*Withdrawal Request*") Then ws.Cells(i, 4).EntireRow.Delete

Next i

【问题讨论】:

  • 布尔条件 = 类似条件 1 或类似条件 2 或类似条件 3 ...等。如果(条件)EntireRow.Delete.
  • 注意 - 你有这个的方式,也许这就是你需要的,但如果Cells(2,4)SSI,那么它将删除该行。然后它将再次查看Cells(2,4),检查其中是否有Settlement instruction,如果有,请删除,然后再次查看下一个...等.
  • 还有VBA.Filter()函数。 see here

标签: excel vba wildcard vb-like-operator


【解决方案1】:

请注意:

  1. 删除行后,单元格将向上移动,因此下面的行将有效地具有相同的行号,这就是为什么我在删除行 /e 后使用 do 循环而不是 i = i - 1:是的,我可以使用步骤 -1 或以不同的方式写它,但想展示与其他答案不同的东西
  2. 也不需要使用 like 运算符,vbs 不支持它,因此如果您决定学习 vbs,最好避免使用它

这是我的方法,您可以继续向数组中添加更多关键字,或者改为创建集合:

MyArr = Array("SSI", "Settlement instruction", "delivery Instruction", "Request form", "Sales to onboarding", "Application", "Doc Check list", "Prime to Credit", "Prime to Legal", "Prime_Legal", "Prime_Credit", "LEXIS", "Withdrawal Request")

LastRow = ws.Range("E1").CurrentRegion.Rows.count
i = 1
Do Until i > LastRow
    i = i + 1
    cVal = ws.Cells(i, 4).Value
    For Each ma In MyArr
        If InStr(1, cVal, ma) > 0 Then
            ws.Cells(i, 4).EntireRow.Delete
            i = i - 1 'cells below will shift up, so next row will have the same row number
            Exit For
        End If
    Next
Loop

【讨论】:

    【解决方案2】:

    有很多方法可以做到这一点,但这里有一个:

    首先,删除行时,始终从范围的底部开始并向上移动 - 这样可以防止在删除时跳过行。

    我通过使用逗号分割文本创建了一个数组。如果您的数据可能包含逗号,则需要更改它。

    Dim tmpAr As Variant
    Dim test As Variant
    
    Set ws = ActiveSheet
    tmpAr = Split("SSI,Settlement instruction,delivery Instruction,Request form,Sales to onboarding,Application,Doc Check list,Prime to Credit,Prime to Legal,Prime_Legal,Prime_Credit,LEXIS,Withdrawal Request", ",")
    For i = ws.Range("E1").CurrentRegion.Rows.Count To 2 Step -1
        For Each test In tmpAr
            If ws.Cells(i, 4).Value Like "*" & test & "*" Then
                ws.Cells(i, 4).EntireRow.Delete
                Exit For
            End If
        Next
    Next i
    

    【讨论】:

    • 我应该如何定义 tmpAr?变种?细绳?谢谢。
    • 还要测试吗?我不熟悉范围。谢谢
    • 答案中添加了声明。将它们创建为 Variant,然后由 SplitFor each.. 完成其余的工作。
    【解决方案3】:
    1. 向后运行循环
    2. 避免重新计算
    3. 只删除一次


    For i = ws.Range("E1").CurrentRegion.Rows.Count To 2 Step -1
        DR = False
        Set r = ws.Cells(i, 4)
        s = r.Value
        If s Like ("*SSI*") Then DR = True
        If s Like ("*Settlement instruction*") Then DR = True
        If s Like ("*delivery Instruction*") Then DR = True
        If s Like ("*Request form*") Then DR = True
        If s Like ("*Sales to onboarding*") Then DR = True
        If s Like ("*Application*") Then DR = True
        If s Like ("*Doc Check list*") Then DR = True
        If s Like ("*Prime to Credit*") Then DR = True
        If s Like ("*Prime to Legal*") Then DR = True
        If s Like ("*Prime_Legal*") Then DR = True
        If s Like ("*Prime_Credit*") Then DR = True
        If s Like ("*LEXIS*") Then DR = True
        If s Like ("*Withdrawal Request*") Then DR = True
        If DR Then ws.Cells(i, 4).EntireRow.Delete
    Next i
    

    【讨论】:

      【解决方案4】:

      你可以试试这些方法

      Sub del()
      
      Dim a As Variant
      Dim s As Variant
      Dim r As Range
      Dim l As Long
      
      a = Array("*abc*", "*def*", "efg*", "abcdef*hi")
      Set r = Range("a1:a20")
      
      For l = r.Rows.Count To 1 Step -1
      
          For Each s In a
              If r.Cells(l, 1).Value Like s Then
                      Rows(l).EntireRow.Delete
                      Exit For
              End If
          Next s
      
      Next l
      
      End Sub
      

      【讨论】:

      • 您也可以在 excel 中使用范围来填充数组 a,并使用 r 范围来填充循环的开始和结束。
      猜你喜欢
      • 1970-01-01
      • 2019-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-11
      • 2019-01-18
      • 1970-01-01
      • 2021-12-11
      相关资源
      最近更新 更多