Solved

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

Posted on 2013-06-11
3
441 Views
Last Modified: 2013-06-11
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

0
Comment
Question by:crepe
3 Comments
 
LVL 39

Accepted Solution

by:
nutsch earned 500 total points
ID: 39238921
if there are no visible cells, your code will crash. You can use a On error resume next line, or you could test for lines before you run your copy, e.g. with a subtotal(3 (count), as in:

if application.worksheetfunction.subtotal(3,columns(1))>1 then
.Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row).SpecialCells(xlCellTypeVisible).Copy _
        Sheets("Errors List").Range("B11")
end if

Open in new window


Thomas
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 39238990
Thomas, I'll have to remember that trick.  I typically just use On Error Resume Next :)
0
 

Author Closing Comment

by:crepe
ID: 39239022
Perfect! Thanks!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

What is a Form List Box? (skip if you know this) The forms List Box is the alternative to the ActiveX list box. If you are using excel 2007, you first make sure you have a developer tab (click the Orb)->"Excel Options"->Popular->"Show Developer tab…
Drop Down List with Unique/Distinct Values (enhancing the Combo-Box with a few steps and a little code) David miller (dlmille) Intro Have you ever created a data validation list from a database field or spreadsheet column (e.g., Zip Codes or Co…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now