如果字符串包含case语句

时间:2016-01-17 22:23:07

标签: excel vba case contains

我正在尝试做一个select case语句,它基于我存储在变量中的字符串包含某些单词。从我到处看到的,我看起来我做得对,但它不起作用。我在变量“Model”中继续得到“编译错误:无效限定符”,在case语句中,即Case Model.Contains(“GR CHER”)。

有没有人看到我在这里遗失的东西?

i = 2
Model = Sheets("Data").Cells(i, 8)

While Not IsEmpty(Cells(i, 1))
    Select Case True
        Case Model.Contains("GR CHER")
            Model = "Grand Cherokee"
        Case Model.Contains("CHRGR")
            Model = "Charger"
        Case Model.Contains("HLLCT")
            Model = "HellCat"
        Case Model.Contains("R1500")
            Model = "Ram 1500"
        Case Else
            Rows([i]).EntireRow.Delete
            i = i - 1
    End Select

    Sheets("Data").Cells(i, 8).Value = Model

    i = i + 1
Wend

4 个答案:

答案 0 :(得分:2)

您的代码看起来不像VBA。

另外,我认为你不能做任何等于"包含"是Select Case

您的代码,已重构,还修复了其他一些问题

Sub zx()
    Dim i As Long
    Dim Model As Variant
    Dim Deleted As Boolean
    i = 2

    With Sheets("Data")
        Do While Not IsEmpty(Sheets("Data").Cells(i, 1))
            Model = Sheets("Data").Cells(i, 8)
            Deleted = False
            If Model Like "*GR CHER*" Then
                Model = "Grand Cherokee"
            ElseIf Model Like "*CHRGR*" Then
                Model = "Charger"
            ElseIf Model Like "*HLLCT*" Then
                Model = "HellCat"
            ElseIf Model Like "*R1500*" Then
                Model = "Ram 1500"
            Else
                Rows(i).EntireRow.Delete
                Deleted = True
            End If
            If Not Deleted Then
                .Cells(i, 8).Value = Model
                i = i + 1
            End If
        Loop
    End With
End Sub

答案 1 :(得分:2)

无需

  1. 遍历每一行以删除 - 更快到AutoFilter
  2. 或者在您不使用它的每一步中派生并存储变量 Model
  3. 您可以尝试以下方法,即VBA等效

    • 在工作栏中输入公式以重新调整TRUE或FALSE以测试H列中的每个字符串
    • 过滤掉错误结果

    =COUNT(SEARCH({"*GR CHER*","*CHRGR*","*HLLC*","*R1500*"},I2))=1

    Sub QuickCull()
    Dim ws As Worksheet
    Dim rng1 As Range
    Set ws = Sheets("Data")
    Set rng1 = ws.Range(ws.[b2], ws.Cells(Rows.Count, "B").End(xlUp))
    
    Application.ScreenUpdating = False
    
    With rng1.Offset(0, 1)
         .EntireColumn.Insert
         .FormulaR1C1 = "=COUNT(SEARCH({""*GR CHER*"",""*CHRGR*"",""*HLLC*"",""*R1500*""},RC[5]))=1"
         .AutoFilter Field:=1, Criteria1:="False"
         .Offset(1, 0).EntireRow.Delete
         .EntireColumn.Delete
    End With
    
    Application.ScreenUpdating = True
    
    End Sub
    

答案 2 :(得分:0)

正如克里斯所说,你不能使用"包含",这个功能将是" InStr"但我很确定以下方法可以完成工作。

Sub Models()
    Dim i As Long: i = 2
    With ThisWorkbook.Sheets("Data")
        While i <= .Cells(.Rows.Count, "A").End(xlUp).Row
            If .Cells(i, 8) Like "*GR CHER*" Then
                .Cells(i, 8) = "Grand Cherokee"
                i = i + 1
            ElseIf .Cells(i, 8) Like "*CHRGR*" Then
                .Cells(i, 8) = "Charger"
                i = i + 1
            ElseIf .Cells(i, 8) Like "*HLLCT*" Then
                .Cells(i, 8) = "HellCat"
                i = i + 1
            ElseIf .Cells(i, 8) Like "*R1500*" Then
                .Cells(i, 8) = "Ram 1500"
                i = i + 1
            Else
                .Rows(i).EntireRow.Delete
            End If
        Wend
    End With
End Sub

答案 3 :(得分:0)

看起来它正在寻找完全匹配。如果我有  单元格中的“AVENGER(3.6L)” 并且代码说:

If Model Like "*AVENGER*" Then 
    Model = "Avenger"
    Deleted = False

或者如果我尝试:

If Model Like "AVENGER" Then 
    Model = "Avenger"
    Deleted = False

它不会捕获那个If语句,并且会一直向下直到else语句并删除行