Thanks Thanks:  0
Likes Likes:  0
Results 1 to 4 of 4

Thread: VBA Ignoring On Error GoTo Command.. XL 2010

  1. #1
    New Member
    Join Date
    Sep 2015
    Posts
    3
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default VBA Ignoring On Error GoTo Command.. XL 2010

    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
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, _
            Transpose:=False
        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?

  2. #2
    Moderator mole999's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    9,093
    Post Thanks / Like
    Mentioned
    10 Post(s)
    Tagged
    1 Thread(s)

    Default Re: VBA Ignoring On Error GoTo Command.. XL 2010

    If its truly empty then I've had issues with paste special, nothing there to paste hence error
    • Yes I know there are better ways to do it. I just wish I knew them. - 2003, 2007, 2010, 2013 & 2016
    • I wear my ignorance openly, excel is not my chosen career, its a means to an ends
    • Posting Guidelines Want to post well laid out questions and answers Translate Excel Versions
      Code:
      [CODE ]Put Your Code[/ CODE]
    • Settings > General Settings (on the left) scroll to the bottom, > Miscellaneous Options > Use ENHANCED
    • X-Posting Guidelines Rule 13 > CHART STUFF

  3. #3
    New Member
    Join Date
    Sep 2015
    Posts
    3
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: VBA Ignoring On Error GoTo Command.. XL 2010

    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

  4. #4
    MrExcel MVP
    Moderator
    RoryA's Avatar
    Join Date
    May 2008
    Location
    UK
    Posts
    32,351
    Post Thanks / Like
    Mentioned
    25 Post(s)
    Tagged
    3 Thread(s)

    Default Re: VBA Ignoring On Error GoTo Command.. XL 2010


Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •