Share via

error message and exit sub

Anonymous
2012-04-27T08:22:56+00:00

Hi, I am trying to create an error message in my code if the user did not copy any infomation which results in nothing to paste

On Error Resume Next

Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _

        :=False, Transpose:=False

If Err Then MsgBox "Nothing has been copied.  Please close and start again!": Err.Clear

This creates my error messge but then continues with the sub until it fails due to the resume next.

I have also tried

On Error GoTo Getout

'code

Getout:

This stops the code but does not give the user an error message.

Preferably, I would like to creat an error message that informs the user of the error and the workbook then closes without saving.

Thanks

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

HansV 462.6K Reputation points
2012-04-27T21:10:38+00:00

Try something like this:

Sub MyMacro()

    On Error GoTo ErrHandler

    ' ...

    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _

         :=False, Transpose:=False

    ' ...

    Exit Sub

ErrHandler:

    MsgBox "Nothing has been copied.", vbCritical

    ActiveWorkbook.Close SaveChanges:=False

End Sub

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful