Avatar of Seamus2626
Seamus2626
Flag for Ireland asked on

Filter and Copy

Hi,

I have a piece of code that filters and copyies data.

The problem is when there is no daa to copy, i get the error

"Object Required"

I thought by saying
        If Not rData Is Nothing Then
        Exit Sub

It would leave the sub but it doesnt!

Can anyone advise?

Thanks
Seamus
With ActiveSheet
    .AutoFilterMode = False
    .Range("A1").AutoFilter Field:=8, Criteria1:="CA"
    With .AutoFilter.Range
        On Error Resume Next
        Set rData = .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
        On Error GoTo 0
        If Not rData Is Nothing Then
        Exit Sub
        Else
            rData.EntireRow.Copy
        End If
    End With

Open in new window

Microsoft Excel

Avatar of undefined
Last Comment
Seamus2626

8/22/2022 - Mon
Norie

Seamus

You've got the logic the wrong way round.

It should be:


If Not rData Is Nothing Then

      rData.EntireRow.Copy
Else

      Exit sub
End If

Open in new window

ASKER CERTIFIED SOLUTION
Patrick Matthews

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Seamus2626

ASKER
Thanks imnorie, but  that gave me the same result

Thank you Matthewspatrick, that worked well

Seamus
Your help has saved me hundreds of hours of internet surfing.
fblack61