Select 具有多个 "Like" 条件的案例
Select Case with multiple "Like" conditions
我正在尝试通过创建较小的 Case statement
来减少较大的 If statement
。我不确定我做错了什么,但这是我目前所知道的。
它类似于this post,但没有解决我的问题的多种情况和一种结果。
With tempWB.Worksheets(1)
rwCnt = .cells(Rows.Count, 1).End(xlup).Row
.Rows(rwCnt).Delete shift:=xlShiftUp
rwCnt = rwCnt - 1
For y = rwCnt to 2 step -1
'Delete Non-Individuals
Select Case .Cells(y, 1).Value2
Case (.Cells(y, 1).Value2 Like ("* TRUST *" Or "* AND *" Or "* & *" Or "* OF *" Or _
"* LLC*" Or "* REV TR *" Or "* LV TR *" Or "* BY *" Or "*'S *" Or "C/O*"))
.Rows(y).Delete shift:=xlShiftUp
End Select
Next y
' If .Cells(y, 1).Value2 Like "* TRUST *" Then
' .Rows(y).Delete shift:=xlShiftUp
' ElseIf .Cells(y, 1).Value2 Like "* AND *" Then
' .Rows(y).Delete shift:=xlShiftUp
' ElseIf .Cells(y, 1).Value2 Like "* & *" Then
' .Rows(y).Delete shift:=xlShiftUp
' ElseIf .Cells(y, 1).Value2 Like "* OF *" Then
' .Rows(y).Delete shift:=xlShiftUp
' ElseIf .Cells(y, 1).Value2 Like "* LLC*" Then
' .Rows(y).Delete shift:=xlShiftUp
' ElseIf .Cells(y, 1).Value2 Like "* REV TR *" Then
' .Rows(y).Delete shift:=xlShiftUp
' ElseIf .Cells(y, 1).Value2 Like "* LV TR *" Then
' .Rows(y).Delete shift:=xlShiftUp
' ElseIf .Cells(y, 1).Value2 Like "* BY *" Then
' .Rows(y).Delete shift:=xlShiftUp
' ElseIf .Cells(y, 1).Value2 Like "*'S *" Then
' .Rows(y).Delete shift:=xlShiftUp
' ElseIf .Cells(y, 1).Value2 Like "C/O*" Then
' .Rows(y).Delete shift:=xlShiftUp
' End If
End With
Case statement
下方的 If statement
效果很好,它看起来很笨重,我认为 Case statement
会稍微简化一些事情。我只是不是 100% 确定如何实施它。提前致谢。
评论中指出的用例为真。
Select Case True
Case .Cells(y, 1).Value2 Like "* TRUST *" _
Or .Cells(y, 1).Value2 Like "* AND *" _
Or .Cells(y, 1).Value2 Like "* & *" _
Or .Cells(y, 1).Value2 Like "* OF *" _
Or .Cells(y, 1).Value2 Like "* LLC*" _
Or .Cells(y, 1).Value2 Like "* REV TR *" _
Or .Cells(y, 1).Value2 Like "* LV TR *" _
Or .Cells(y, 1).Value2 Like "* BY *" _
Or .Cells(y, 1).Value2 Like "*'S *" _
Or .Cells(y, 1).Value2 Like "C/O*"
.Rows(y).Delete shift:=xlShiftUp
End Select
另一种方法是编写一个函数来迭代条件:
Function Likes(Value As Variant, ParamArray Conditions() As Variant)
Dim Condition
For Each Condition In Conditions
If Value Like Condition Then
Likes = True
Exit Function
End If
Next
End Function
用法
If Likes(.Cells(y, 1).Value2, "* TRUST *", "* AND *", "* & *", "* OF *", "* LLC*", "* REV TR *", "* LV TR *", "* BY *", "*'S *", "C/O*") Then
.Rows(y).Delete shift:=xlShiftUp
End If
您不能像您尝试的那样将 Like
比较的右侧串在一起。
此外,如链接 post 中所述,您需要使用 Select Case True
,因为 Like
比较的结果是 Boolean
.
Select Case
看起来像这样:
Select Case True
Case .Cells(y, 1).Value2 Like "* TRUST *", _
.Cells(y, 1).Value2 Like "* AND *", _
.Cells(y, 1).Value2 Like "* & *", _
'... and so on
.Rows(y).Delete shift:=xlShiftUp
End Select
我正在尝试通过创建较小的 Case statement
来减少较大的 If statement
。我不确定我做错了什么,但这是我目前所知道的。
它类似于this post,但没有解决我的问题的多种情况和一种结果。
With tempWB.Worksheets(1)
rwCnt = .cells(Rows.Count, 1).End(xlup).Row
.Rows(rwCnt).Delete shift:=xlShiftUp
rwCnt = rwCnt - 1
For y = rwCnt to 2 step -1
'Delete Non-Individuals
Select Case .Cells(y, 1).Value2
Case (.Cells(y, 1).Value2 Like ("* TRUST *" Or "* AND *" Or "* & *" Or "* OF *" Or _
"* LLC*" Or "* REV TR *" Or "* LV TR *" Or "* BY *" Or "*'S *" Or "C/O*"))
.Rows(y).Delete shift:=xlShiftUp
End Select
Next y
' If .Cells(y, 1).Value2 Like "* TRUST *" Then
' .Rows(y).Delete shift:=xlShiftUp
' ElseIf .Cells(y, 1).Value2 Like "* AND *" Then
' .Rows(y).Delete shift:=xlShiftUp
' ElseIf .Cells(y, 1).Value2 Like "* & *" Then
' .Rows(y).Delete shift:=xlShiftUp
' ElseIf .Cells(y, 1).Value2 Like "* OF *" Then
' .Rows(y).Delete shift:=xlShiftUp
' ElseIf .Cells(y, 1).Value2 Like "* LLC*" Then
' .Rows(y).Delete shift:=xlShiftUp
' ElseIf .Cells(y, 1).Value2 Like "* REV TR *" Then
' .Rows(y).Delete shift:=xlShiftUp
' ElseIf .Cells(y, 1).Value2 Like "* LV TR *" Then
' .Rows(y).Delete shift:=xlShiftUp
' ElseIf .Cells(y, 1).Value2 Like "* BY *" Then
' .Rows(y).Delete shift:=xlShiftUp
' ElseIf .Cells(y, 1).Value2 Like "*'S *" Then
' .Rows(y).Delete shift:=xlShiftUp
' ElseIf .Cells(y, 1).Value2 Like "C/O*" Then
' .Rows(y).Delete shift:=xlShiftUp
' End If
End With
Case statement
下方的 If statement
效果很好,它看起来很笨重,我认为 Case statement
会稍微简化一些事情。我只是不是 100% 确定如何实施它。提前致谢。
评论中指出的用例为真。
Select Case True
Case .Cells(y, 1).Value2 Like "* TRUST *" _
Or .Cells(y, 1).Value2 Like "* AND *" _
Or .Cells(y, 1).Value2 Like "* & *" _
Or .Cells(y, 1).Value2 Like "* OF *" _
Or .Cells(y, 1).Value2 Like "* LLC*" _
Or .Cells(y, 1).Value2 Like "* REV TR *" _
Or .Cells(y, 1).Value2 Like "* LV TR *" _
Or .Cells(y, 1).Value2 Like "* BY *" _
Or .Cells(y, 1).Value2 Like "*'S *" _
Or .Cells(y, 1).Value2 Like "C/O*"
.Rows(y).Delete shift:=xlShiftUp
End Select
另一种方法是编写一个函数来迭代条件:
Function Likes(Value As Variant, ParamArray Conditions() As Variant)
Dim Condition
For Each Condition In Conditions
If Value Like Condition Then
Likes = True
Exit Function
End If
Next
End Function
用法
If Likes(.Cells(y, 1).Value2, "* TRUST *", "* AND *", "* & *", "* OF *", "* LLC*", "* REV TR *", "* LV TR *", "* BY *", "*'S *", "C/O*") Then .Rows(y).Delete shift:=xlShiftUp End If
您不能像您尝试的那样将 Like
比较的右侧串在一起。
此外,如链接 post 中所述,您需要使用 Select Case True
,因为 Like
比较的结果是 Boolean
.
Select Case
看起来像这样:
Select Case True
Case .Cells(y, 1).Value2 Like "* TRUST *", _
.Cells(y, 1).Value2 Like "* AND *", _
.Cells(y, 1).Value2 Like "* & *", _
'... and so on
.Rows(y).Delete shift:=xlShiftUp
End Select