excelvbaswitch-statementselect-case

Select Case with multiple "Like" conditions


I am trying to reduce a large If statement by creating a smaller Case statement. I am not sure what I am doing wrong, but here is what I have so far.

It is similar to this post, but doesn't address the multiple situations and one result of my issue.

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

Below the Case statement is the If statement that works great, it just looks clunky and I think a Case statement would simplify things a bit. I'm just not 100% sure how to implement it. Thanks in advance.


Solution

  • You can't string together the right side of that Like comparison like you're attempting to.

    Also, as noted in the linked post, you need to use Select Case True, because the result of a Like comparison is a Boolean.

    The Select Case could then look something like this:

    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