+ Reply to Thread
Results 1 to 5 of 5

On Error GoTo not working as expected

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-27-2012
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    290

    On Error GoTo not working as expected

    This section of code is causing an error:

        On Error GoTo EndMacro
        Range("A2:I" & Lastrow).SpecialCells(xlCellTypeVisible).Copy
    the error is: "No Cells Were Found"

    and that is ok, its what I expect to happen. So If this error happens, I want to redirect the code to the end of the macro but "GoTo EndMacro" isnt doing that for some reason. I have that Label in my script but the code just doesnt want to be redirected to it.

    any1 know why? is there a simple solution?

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: On Error GoTo not working as expected

    Sub errme()
    LastRow= Range("A" & Rows.count).End(xlUp).Row
      If LastRow>=2 Then
        Range("A2:I" & LastRow).SpecialCells(xlCellTypeVisible).Copy
      End If
    End Sub

  3. #3
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,318

    Re: On Error GoTo not working as expected

    Two possibilities spring to mind:
    1. You have already used on On Error Goto statement somewhere without then using a Resume statement.
    2. You have your VBE set to break on all errors.
    Everyone who confuses correlation and causation ends up dead.

  4. #4
    Forum Contributor
    Join Date
    03-27-2012
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    290

    Re: On Error GoTo not working as expected

    @romperstomper yes I have used an "On Error GoTo" statement somewhere without using "Resume"

    I basically have this sort of set up:

    Sub Macro
    do code
    
    do code
    On Error GoTo Part2
    do code
    
    Part2:
    do code
    
    do code
    On Error GoTo EndPath:
    do code
    
    do code
    EndPath
    Do code
    
    End Sub
    where would I put the resume? before the "Part2" section?

  5. #5
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,318

    Re: On Error GoTo not working as expected

    What you should do is rewrite and restructure your code! Impossible to be specific with no actual code to go on, but as a general guideline you should have one error handler per routine (if any).

    The fact that you have all those different Goto statements suggests that you should probably be using multiple routines, or at the very least restructuring the code you have now so that it doesn't leap around so much.

    For your specific visiblecells issue here, I would actually use something like:
    Dim rngVisible as Range
    On Error Resume Next
    Set rngVisible = Range("A2:I" & Lastrow).SpecialCells(xlCellTypeVisible)
    On Error Goto 0
    if rngVisible is Nothing then Exit Sub
    rngVisible.Copy
    ' rest of code

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] On Error Goto statement not working when looping
    By som3on3_10 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-21-2013, 12:58 PM
  2. GoTo compile error: "Expected: line number or label"
    By elfsprin in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-29-2011, 09:21 PM
  3. [SOLVED] On Error GoTo Label in a loop only working once.
    By Ken Johnson in forum Excel Charting & Pivots
    Replies: 4
    Last Post: 07-05-2006, 04:45 PM
  4. Error Handling - On Error GoTo doesn't trap error successfully
    By David in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-16-2006, 02:10 PM
  5. [SOLVED] Lookup Not working as expected
    By trumpy81 in forum Excel - New Users/Basics
    Replies: 5
    Last Post: 06-30-2005, 03:05 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1