Excel VBA中的IF语句中的多个条件“如果不加阻止则结束”

时间:2019-03-20 22:08:40

标签: excel vba compiler-errors conditional-statements

我是VBA的总初学者,我正在从不必要的数据(包含.jpg,.gif等的行中清除Web日志)中找到可以完全满足我需要的代码,但是我得到了错误“编译错误:如果没有块则结束,如果”

Sub clean() 
Dim r As Long, endRow As Long, pasteRowIndex As Long

endRow = 321085 

For r = endRow To 1 Step -1  
  Select Case Cells(r, Columns("F").Column).Value
        Case "*.jpg*", "*.JPG*", "*.gif*"
            Rows(r).Delete
        Case Else
            ' Do nothing...

    End Select

    End If
 Next r
End Sub

1 个答案:

答案 0 :(得分:0)

要对Select Case使用通配符,可以使用Like

Sub clean() 
Dim r As Long, endRow As Long, pasteRowIndex As Long

' I assume your column F has the most data
endRow = Cells(Rows.Count, "F").End(xlUp).Row 

For r = endRow To 1 Step -1  
  Select Case True
        Case Cells(r, "F").Value Like "*.jpg*" or Cells(r, "F").Value Like "*.JPG*" or Cells(r, "F").Value Like "*.gif*"
            Rows(r).Delete
        Case Else
            ' Do nothing...
    End Select
 Next r
End Sub

编辑:通过将要查找的值放入数组中,然后搜索该数组,可能是一种更好的方法,但以上方法应适用于当前方法。