如果错误,则转到***,否则

时间:2018-08-24 12:52:11

标签: excel vba excel-vba

我遇到一个问题,如果没有要粘贴的内容,我要转到Err1:如果没有,我要继续粘贴。

这是我的代码,但是即使有粘贴,它也总是跳转到Err1 :。

Selection.Copy

On Error Resume Next
Sheet2.Range("A3").Paste
'~~~~> Want to skip to Err1: which will display a msgbox if nothing to paste
If Err Then GoTo Err1:
'~~~~> Want to continue here if there is something to paste
Range("BC3:BF3").Select
Range("BC3:BF3").Select
Application.CutCopyMode = False
Selection.AutoFill Destination:=Range("BC3:BF142")
Sheet3.Range("B8").Select
ActiveWorkbook.RefreshAll
Range("I7").Select
ActiveWorkbook.RefreshAll

1 个答案:

答案 0 :(得分:6)

您的错误处理应如下所示:

Option Explicit

Sub MyProcedure()

    On Error GoTo PASTE_ERROR:

    Sheet2.Range("A3").Paste
    '~~~~> Want to skip to Err1: which will display a msgbox if nothing to paste

    On Error GoTo 0 'back to default error handling

    'other code

    Exit Sub 'exit here if no error
PASTE_ERROR:
    MsgBox "Paste Error"
End Sub

另外,我建议阅读How to avoid using Select in Excel VBA