mha*_*erk 1 excel vba excel-vba
我有一个问题,如果没有什么要粘贴我想GoTo 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
Run Code Online (Sandbox Code Playgroud)
您的错误处理应该像这样工作:
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
Run Code Online (Sandbox Code Playgroud)
另外,我建议阅读如何避免在Excel VBA中使用Select.