Consulting

Results 1 to 5 of 5

Thread: Error Handling

  1. #1

    Error Handling

    I have been working with the following Sub to delete a module from the ThisWorkbook. When the code is there, it works fine. But if it is not there, I get an error 35 which is fine. I thought by placing error handling code and having the sub exit would work. But it still crashes the sub and throws the error. What am I doing wrong?
    I want the seb deleted if it is there and do nothing if it is not.


    [vba]Sub DeleteProcedure()
    ' Delete module
    Dim VBCodeMod As CodeModule
    Dim StartLine As Long
    Dim HowManyLines As Long
    On Error GoTo ErrorHandler
    Set VBCodeMod = ActiveWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
    With VBCodeMod
    StartLine = .ProcStartLine("Workbook_Open", vbext_pk_Proc)
    HowManyLines = .ProcCountLines("Workbook_Open", vbext_pk_Proc)
    .DeleteLines StartLine, HowManyLines
    End With
    ErrorHandler:

    End Sub[/vba]

  2. #2
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Just test for its existence first...[VBA]
    Sub DeleteProcedure()

    Dim StartLine As Long
    Dim HowManyLines As Long

    With ActiveWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
    StartLine = .CountOfDeclarationLines + 1
    Do Until StartLine >= .CountOfLines
    If .ProcOfLine(StartLine, vbext_pk_Proc) = "Workbook_Open" Then
    HowManyLines = .ProcCountLines("Workbook_Open", vbext_pk_Proc)
    .DeleteLines StartLine, HowManyLines
    End If
    Loop
    End With
    End Sub
    [/VBA]
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  3. #3
    John,
    Your code works. Thank you. But I am still at a lack of understanding as to why the error handlers did not work. I have read the information on the CPearson website but do not understand why the error is still taking place.
    When you ran my original code, did you get the error? If so, without checking for existance, how would handle the error? How would you handle an unexpected error?

    Thanks for the help,

    Michael

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    For me, StartLine throws an error and branches out.

  5. #5
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by MrRhodes2004
    John,
    Your code works. Thank you. But I am still at a lack of understanding as to why the error handlers did not work. I have read the information on the CPearson website but do not understand why the error is still taking place.
    When you ran my original code, did you get the error? If so, without checking for existance, how would handle the error? How would you handle an unexpected error?

    Thanks for the help,

    Michael
    Michael,

    No, I don't get an error with your original code.

    I would probably handle an unexpected error in the same way you did, however, this is not really an unexpected error, it's an easily anticipated one that can (and should) be catered for...

    Another way to cater for it - seeing the code is intended for a 'once-only' use - would be to have the procedure delete itself after deleting the Workbook_Open procedure.

    Regards,
    John
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

Posting Permissions

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