Excel VBA中IF语句中的多个条件“if if without block if”

问题描述 投票:0回答:1

我是VBA的初学者,我正在努力从不必要的数据中清除网络日志(包含.jpg,.gif,...的行)我找到的代码完全符合我的需要,但是我得到了ERROR“编译错误:如果没有阻止则结束“

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
excel vba compiler-errors condition
1个回答
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

编辑:可能有更好的方法,通过将您想要在数组中找到的值,然后搜索数组,但上面应该使用当前方法。

© www.soinside.com 2019 - 2024. All rights reserved.