VBA Ignoring On Error GoTo Command.. XL 2010

jxd078

New Member
Joined
Sep 18, 2015
Messages
3
Hi,

Relatively new to VBA, but having read up on using On Error GoTo commands I cannot understand how the code is not responding when it encounters an error.
The most annoying part is to get to that part of the code it has already correctly been navigated using an On Error statement.
I'm sure it's really just to do with how I've structured/ordered my code.

Code:
Function PasteUS()


On Error GoTo NextTry
ActiveSheet.PasteSpecial Format:="SYLK", Link:=False, DisplayAsIcon:= _
        False


LetsContinue:
Exit Function


NextTry:
On Error GoTo ErrHandler
[COLOR=#ff0000]Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, _[/COLOR]
[COLOR=#ff0000]        Transpose:=False[/COLOR]
    Application.CutCopyMode = False
Resume LetsContinue


ErrHandler:
MsgBox errstring & " " & Err.Description
Resume LetsContinue


End Function

The error context:

User is copying data from an external source.

In the sub which calls this function, there is a function that checks if the clipboard is empty, so to get to this code VBA has deemed the clipboard is NOT empty.

However the error message only occurs when it seems like nothing has been copied. For example copy an area of data in another instance of excel (which is subsequently surrounded by the black and white moving border).
If you press Esc on the keyboard, the border disappears, and returns to the range just being a highlighted selection.

But stepping through the macro, the clipboard passes the not empty test, then errors when it gets to the red text in the code above.
The error is 'PasteSpecial method of Range class failed'.

Any ideas why the code is not being redirected to the Error Handler when this error occurs?
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
If its truly empty then I've had issues with paste special, nothing there to paste hence error
 
Upvote 0
I understand why the error is occurring, the real question is why that doesn't prompt it to move to the error handler, as directed
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,774
Members
452,353
Latest member
strainu

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