Home » excel » excel database function in combination with vba, what if there are no records?

excel database function in combination with vba, what if there are no records?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m using the database function of excel. see example image

enter image description here

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! 🙂

How to&Answers:

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

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

Answer:

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