Vba 如果存在自动筛选结果,则要复制/粘贴的宏

Vba 如果存在自动筛选结果,则要复制/粘贴的宏,vba,excel,Vba,Excel,我有以下代码。我正在应用一个具有不同条件的过滤器,当过滤器有行时它会工作,但如果没有,我会在“My_Range.Parent.AutoFilter.Range.Copy”行中出现错误“Property let procedure not defined and Property get procedure not returnal an object” 如何跳过此空“粘贴”继续执行宏?谢谢 My_Range.AutoFilter Field:=14, Criteria1:="=FEDERAL"

我有以下代码。我正在应用一个具有不同条件的过滤器,当过滤器有行时它会工作,但如果没有,我会在“My_Range.Parent.AutoFilter.Range.Copy”行中出现错误“Property let procedure not defined and Property get procedure not returnal an object”

如何跳过此空“粘贴”继续执行宏?谢谢

My_Range.AutoFilter Field:=14, Criteria1:="=FEDERAL"
My_Range.AutoFilter Field:=7, Criteria1:="=No"


    'Copy/paste the visible data to the new worksheet

    My_Range.Parent.AutoFilter.Range.Copy
        With Sheets("Federal").Range("c5")
        .PasteSpecial xlPasteValues
        .PasteSpecial xlPasteFormats
        Application.CutCopyMode = False
        '.Select
    End With


End If

跳过错误的简单方法是写入:

On Error GoTo myerrorhandler 'put this line on the beginning of you sub/function
     ... your code ...
myerrorhandler: 'after error your code continues to execute from this line
     .... more of your code...
或者你可以直接写

On Error Resume Next 'your code will resume to next line of code after the error

顺便说一句,注意这也会禁用所有错误消息,这意味着您应该确保您的代码在实现之前没有任何其他错误或bug。如果您希望进行调试或类似操作,这也将“隐藏”所有错误。。