简体   繁体   中英

When autofilter returns nothing, clear filter and end macro

I have this macro that runs a filter and delete the results, but today the filter results brought back nothing and i got an error. I want to make it so that when the filter in the AV range results in nothing, just clear the filter and end sub

this problem occurs after the "Em Aberto- 00" filter.

I have tried some suggestions, but being a noob that i am, most of them i could'nt understand what was going on

Columns("J:J").Select
Selection.TextToColumns Destination:=Range("J1"), DataType:=xlDelimited, _
    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
    Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
    :=Array(1, 4), TrailingMinusNumbers:=True
Columns("K:K").Select
Selection.TextToColumns Destination:=Range("K1"), DataType:=xlDelimited, _
    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
    Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
    :=Array(1, 4), TrailingMinusNumbers:=True

Columns("BD:BF").Select
Selection.Replace What:="", Replacement:="0", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False


Range("A1").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Selection.AutoFilter
ActiveWindow.SmallScroll ToRight:=31
Range("AV3").Select
ActiveSheet.Range("$A$1:$BK$3018").AutoFilter Field:=48, Criteria1:= _
    "Em Aberto- 00"

Range("A2").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.EntireRow.Delete
ActiveSheet.ShowAllData
End Sub

Do something like this:

Dim myRange As Range

On Error Resume Next
Set myRange = Range("your range here").SpecialCells(xlVisible)
On Error GoTo 0

If myRange Is Nothing Then
    Exit Sub
Else
'do stuff
End If

You should avoid using SELECT , just Google "how to avoid using SELECT VBA"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM