Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA:如何对值列表使用like运算符?_Excel_Vba_Wildcard_Vb Like Operator - Fatal编程技术网

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

Excel VBA:如何对值列表使用like运算符?,excel,vba,wildcard,vb-like-operator,Excel,Vba,Wildcard,Vb Like Operator,这是我的部分代码。有没有办法让它变得简单?多谢各位 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

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

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

你可以试着用这些方法

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

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

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

我已经创建了一个数组,使用逗号将文本拆分。如果您的数据可能包含逗号,则需要对其进行更改

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

请注意:

  • 当删除行时,单元格将向上移动,因此下面的行将有效地具有相同的行号,这就是为什么我在删除行后使用do loop而不是
    I=I-1
    :是的,我可以使用步骤-1或以不同的方式编写它,但希望显示与其他答案不同的内容
  • 也不需要使用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
    
  • 向后运行循环
  • 避免重新计算
  • 只删除一次


  • 布尔条件=类似条件1或类似条件2或类似条件3…等等。如果(condition)EntireRow.Delete.Note-您的方式是这样的,也许这就是您所需要的,但是如果
    单元格(2,4)
    具有
    SSI
    ,则它将删除该行。然后它将再次查看
    单元格(2,4)
    ,检查其中是否有
    结算指令
    ,如果有,则删除,然后再次查找下一个……等等。还有
    VBA.Filter()
    函数。您可以使用excel中的一个范围来填充数组
    a
    ,也可以使用
    r
    范围来填充循环的开始和结束。我应该如何定义tmpAr?变体?一串谢谢。还有考试吗?我不熟悉这个范围。谢谢你在回答中添加了声明。将它们创建为
    Variant
    ,其余部分由
    Split
    为每个..
    执行。
    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