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: 
I like doing it this way because you don't need to error check it. If there are no results, it will simply paste a blank cell:

```
Sub tgr()

    With Range("B2").CurrentRegion
        .AutoFilter 2, "yes"
        Intersect(.Offset(1), Columns("B")).Copy Range("B12")
        .AutoFilter
    End With

End Sub

```

Alternately, if you only have one criteria, you could use Countif to test if the criteria exists before performing the filter:

```
Sub tgr()

    Dim strCriteria As String

    strCriteria = "yes"

    With Range("B2").CurrentRegion
        If WorksheetFunction.CountIf(Intersect(.Cells, Columns("C")), strCriteria) > 0 Then
            .AutoFilter 2, strCriteria
            Intersect(.Offset(1), Columns("B")).Copy Range("B12")
            .AutoFilter
        Else
            MsgBox "No cells found to contain """ & strCriteria & """", , "No Matches"
        End If
    End With

End Sub

```
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).