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?
 

mole999

Moderator
Joined
Oct 23, 2004
Messages
9,879
If its truly empty then I've had issues with paste special, nothing there to paste hence error
 

jxd078

New Member
Joined
Sep 18, 2015
Messages
3
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
 

Forum statistics

Threads
1,078,252
Messages
5,339,097
Members
399,277
Latest member
Jyoti C

Some videos you may like

This Week's Hot Topics

Top