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
请注意:
I=I-1
:是的,我可以使用步骤-1或以不同的方式编写它,但希望显示与其他答案不同的内容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