+ Reply to Thread
Results 1 to 5 of 5

What would cause the error handler to not work?

Hybrid View

  1. #1
    Valued Forum Contributor natefarm's Avatar
    Join Date
    04-22-2010
    Location
    Wichita, Kansas
    MS-Off Ver
    2016
    Posts
    1,020

    What would cause the error handler to not work?

    My code opens a user-selected workbook and attempts to select the worksheet name that matches the current sheet in the original workbook. For some reason, the error handler doesn't get triggered if it doesn't find a matching sheet. I have copied the code to a new workbook, and it works fine there. Does anyone have ideas on what might nullify the error handler? In the code below, the FindSheetFailed is the one that won't work. Instead it gets a Subscript Out Of Range error if it doesn't find the sheet.
    Sub CopyAllComments()
        iResult = vbOK
    Restart:
        Call SetDefaultPath
        PathName = Application.GetOpenFilename("Microsoft Office Excel Workbook(*.xls),*.xls", 2, MsgTitle)
        On Error GoTo OpenFailed
        Workbooks.Open PathName
        On Error GoTo 0
    
        On Error GoTo FindSheetFailed
        ActiveWorkbook.Sheets(ThisWorkbook.ActiveSheet.Name).Select
        On Error GoTo 0
    
        Exit Sub
    OpenFailed:
        If PathName <> False Then
            MsgBox "Unable to open " & PathName, vbOKOnly, MsgTitle
        End If
        iResult = vbCancel
        Exit Sub
    FindSheetFailed:
        MsgBox ThisWorkbook.ActiveSheet.Name & " sheet not found on " & ActiveWorkbook.Name
        ThisWorkbook.Activate
        iResult = vbCancel
    End Sub

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: What would cause the error handler to not work?

    Hello natefarm,

    When testing objects it is a good practice to assign the object to an object variable first rather than select it straight away. This method allows you check if the object was set and prevent the system from overriding your error handler. The changes are in bold text.

    Sub CopyAllComments()
      Dim Sht As Object
        iResult = vbOK
    Restart:
        Call SetDefaultPath
        PathName = Application.GetOpenFilename("Microsoft Office Excel Workbook(*.xls),*.xls", 2, MsgTitle)
        On Error GoTo OpenFailed
        Workbooks.Open PathName
        On Error GoTo 0
    
        On Error GoTo FindSheetFailed
          Set Sht = ActiveWorkbook.Sheets(ThisWorkbook.ActiveSheet.Name)
          If Not Sht Is Nothing Then Sht.Select
        On Error GoTo 0
    
        Exit Sub
    OpenFailed:
        If PathName <> False Then
            MsgBox "Unable to open " & PathName, vbOKOnly, MsgTitle
        End If
        iResult = vbCancel
        Exit Sub
    FindSheetFailed:
        MsgBox ThisWorkbook.ActiveSheet.Name & " sheet not found on " & ActiveWorkbook.Name
        ThisWorkbook.Activate
        iResult = vbCancel
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Valued Forum Contributor natefarm's Avatar
    Join Date
    04-22-2010
    Location
    Wichita, Kansas
    MS-Off Ver
    2016
    Posts
    1,020

    Re: What would cause the error handler to not work?

    Thanks for the suggestion. Unfortunately, it didn't help. I still get Subscript Out Of Range.

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: What would cause the error handler to not work?

    Hello natefarm,

    To troubleshoot this further, I would need to see the workbook. Can you post a copy of it?

  5. #5
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: What would cause the error handler to not work?

    You won't need any:
    And you'd better avoid activate and select in VBA.

    Sub CopyAllComments()
      c00=ThisWorkbook.ActiveSheet.Name
      PathName = Application.GetOpenFilename("Microsoft Office Excel Workbook(*.xls),*.xls", 2, MsgTitle)
      if dir(pathname)="" then exit sub
     
      Workbooks.Open PathName
      if not Evaluate("isref(" & c00 & "!A1)") then exit sub
    End Sub



+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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