Solved

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

Posted on 2013-06-11
3
457 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

910 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

24 Experts available now in Live!

Get 1:1 Help Now