Question: I'm using the database function of excel. see example image

![enter image description here](https://i.stack.imgur.com/Had38.png)

I use vba to select records that have 'yes' for lets say A

```
Selection.AutoFilter Field:=2, Criteria1:="yes"
Range("B3").Select
Range(Selection, Selection.End(xlDown)).Select

```

I then copy it to paste it somewhere else. for example:

```
Selection.Copy
Range("B12").Select
ActiveSheet.Paste

```

The problem is that when there are no records with yes, i get error 1004. Probably because there is nothing to paste. How do I write a script so that if there is nothing to paste, it exits the sub?

I tried things like counta but to no succes.

Your help is much appreciated! :)

Answer: 
This will check the number of visible cells after the AutoFilter is applied:

```
Selection.AutoFilter Field:=2, Criteria1:="yes"
If ActiveSheet.AutoFilter.Range.Rows.Offset(1, 0).SpecialCells(xlCellTypeVisible).Count - ActiveSheet.AutoFilter.Range.Columns.Count > 0 Then
    Range("B3").Select
    Range(Range("b3"), Range("b2").End(xlDown)).Select
    Selection.Copy
    Range("B12").Select
    ActiveSheet.Paste
End If

```

The `- ActiveSheet.AutoFilter.Range.Columns.Count` part is to subtract the header cells from the count.

FWIW, when I walked through your original code, I got the 1004 because the Copy area was from B7 to the bottom of the sheet (the effect of xlDown in an empty selection).
You can use the SUBTOTAL worksheet function to count the visible rows and only do the copy and paste if there are visible rows. Here's an example.

```
Sub CopyFiltered()

    Dim rToFilter As Range
    Dim rToCopy As Range
    Dim rToPaste As Range

    Set rToFilter = Selection
    Set rToPaste = rToFilter.Cells(1).Offset(10, 0) 'paste it 10 rows down

    rToFilter.AutoFilter 2, "yes"

    'Use subototal to count the visible rows in column 1
    If Application.WorksheetFunction.Subtotal(2, rToFilter.Columns(1)) > 0 Then
        'Copy excluding the header row
        Set rToCopy = rToFilter.Columns(1).Offset(1, 0).Resize(rToFilter.Rows.Count - 1)
        rToCopy.Copy Destination:=rToPaste
    End If

End Sub

```