VBA:如何使用like运算符到值列表?

问题描述 投票:2回答:4

这是我的代码的一部分。有什么方法可以简单吗?谢谢。

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
excel vba wildcard vb-like-operator
4个回答
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

1
投票

你可以沿着这些方向尝试一些东西

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

1
投票
  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

1
投票

请注意:

  1. 删除行时,单元格将向上移动,因此下面的行将有效地具有相同的行号,这就是为什么我在删除row / 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
© www.soinside.com 2019 - 2024. All rights reserved.