Prevent Error when running Macro

schielrn

Well-known Member
Joined
Apr 4, 2007
Messages
6,941
Range("C8").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("C8").Select

Here is my macro. All I want it to do is take any information that I have copied and paste special it. It does that fine. The problem I have is when there is nothing copied, if someone accidentally pushes the button it gives an error saying PasteSpecial method of Range class failed. Is there a way to prevent this message with like a try catch block or something. I would rather if someone accidentally pushed the button that maybe a message come up that there was nothing copied to the clipboard or something to that effect. I don't know if it is possible in excel.

Thanks,

Rob
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Welcome to the board...

Try using on error goto...

Code:
Sub test()
On Error GoTo Handler
Range("C8").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("C8").Select
Exit Sub
Handler:
MsgBox ("nothing was copied")
End Sub
 
Upvote 0
Hi, schielrn
Welcome to the Board !!!!!

try this
Code:
Sub test()
On Error Resume Next
Range("C8").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
If Err Then MsgBox "your message", vbExclamation, "title"
Range("C8").Select
End Sub
see how I deleted one line: now you will not "go to" C8 if there is an error

kind regards,
Erik
 
Upvote 0

Forum statistics

Threads
1,222,644
Messages
6,167,274
Members
452,108
Latest member
Sabat01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top