【问题标题】:Select Case with multiple "Like" conditions选择具有多个“喜欢”条件的案例
【发布时间】: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 TrueLike 比较的结果是 Boolean。也就是说,您需要重复.Cells(y, 1).Value2,您不能像您尝试的那样将Like 的右侧串在一起。
  • @BigBen - 因为条件使用wildcardlike 我不能让它更短吗?
  • 这是因为Like 比较返回Boolean。通配符与它无关。
  • 它会稍微短一些,因为您只有一个 .Rows(y).Delete shift:=xlShiftUp 实例。
  • 肯定会添加为答案。

标签: excel vba switch-statement select-case


【解决方案1】:

您不能像您尝试的那样将 Like 比较的右侧串在一起。

此外,如链接帖子中所述,您需要使用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

【讨论】:

    【解决方案2】:

    如 cmets 中指出的那样,用例为真。

       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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-28
      相关资源
      最近更新 更多