Link to home
Start Free TrialLog in
Avatar of crepe
crepeFlag for United States of America

asked on

Excel VBA Copying Data with Autofilter Crashes when there is no data to Copy

If there is data, the macro works perfectly but when there's no data in the AutoFilter, it crashes. What did I do wrong and how can I fix it? Thank you!!

' Copies Temperature errors to "Errors List"
With Sheets("All Data").Range("A3:T380")
    .AutoFilter
    .AutoFilter Field:=16, Criteria1:="1"
    .Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row).SpecialCells(xlCellTypeVisible).Copy _
        Sheets("Errors List").Range("F11")
    .AutoFilter
End With
    ActiveSheet.ShowAllData

' Copies Humidity Errors to "Errors List"
With Sheets("All Data").Range("A3:T380")
    .AutoFilter
    .AutoFilter Field:=14, Criteria1:="1"
    .Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row).SpecialCells(xlCellTypeVisible).Copy _
        Sheets("Errors List").Range("D11")
    .AutoFilter
End With
    ActiveSheet.ShowAllData
    
' Copies Operator Errors to "Errors List"
With Sheets("All Data").Range("A3:T380")
    .AutoFilter
    .AutoFilter Field:=11, Criteria1:="Yes"
    .Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row).SpecialCells(xlCellTypeVisible).Copy _
        Sheets("Errors List").Range("B11")
    .AutoFilter
End With
' Remove any filters
ActiveSheet.AutoFilterMode = False

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of nutsch
nutsch
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Patrick Matthews
Thomas, I'll have to remember that trick.  I typically just use On Error Resume Next :)
Avatar of crepe

ASKER

Perfect! Thanks!