【发布时间】:2019-07-17 15:28:57
【问题描述】:
我正在尝试通过创建一个较小的 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。Like比较的结果是Boolean。也就是说,您需要重复.Cells(y, 1).Value2,您不能像您尝试的那样将Like的右侧串在一起。 -
@BigBen - 因为条件使用
wildcard和like我不能让它更短吗? -
这是因为
Like比较返回Boolean。通配符与它无关。 -
它会稍微短一些,因为您只有一个
.Rows(y).Delete shift:=xlShiftUp实例。 -
肯定会添加为答案。
标签: excel vba switch-statement select-case