Consulting

Results 1 to 6 of 6

Thread: Solved: error handling error

  1. #1

    Solved: error handling error

    [CODE]Dim MyFile As String
    MyFile = Dir$("\\a\b\c\d\e\*.xls")
    Do While MyFile <> ""
    On Error GoTo ErrorHandler
    Kill "(

  2. #2
    [VBA]Dim MyFile As String
    MyFile = Dir$("\\a\b\c\d\e\*.xls")Do While MyFile <> ""
    On Error GoTo ErrorHandler
    Kill "\\a\b\c\d\e\" & MyFile
    MyFile = Dir$("\\a\b\c\d\e\*.xls") Loop
    ErrorHandler:
    MsgBox "Please ensure all files in the Reports folder are deleted. Unable to delete " & MyFile & " as readonly."
    If Workbooks(Str_WkbX).Sheets(Str_Wksht_X).ProtectContents = True Then Workbooks(Str_WkbX).Sheets(Str_Wksht_X).Unprotect Password:="54YY4F"
    Workbooks(Str_WkbX).Sheets(Str_Wksht_X).Range("I6").Value = "stopped"
    If Workbooks(Str_WkbX).Sheets(Str_Wksht_X).ProtectContents = False Then Workbooks(Str_WkbX).Sheets(Str_Wksht_X).Protect Password:="54YY4F"
    Exit Sub[/VBA]


    sorry i missed half the post, i am trying to delete all files in a folder and if an error is encountered stop the code and display the message.

    the above always runs the errpr handling.

    what am i doing wrong

  3. #3
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    If you comment out

    [VBA]On Error Goto ErrorHandler [/VBA]

    and run it, what does the debugger tell you? What error do you receive?
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  4. #4
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Quote Originally Posted by khalid79m
    ...the above always runs the errpr handling.

    what am i doing wrong
    Hi there,

    You have the 'Exit Sub' misplaced. It needs to be immediately above 'ErrorHandler'. 'End Sub' should likely be where 'Exit Sub' is currently.

    That way, if no error(s) are encountered, the procedure quits running when it gets to Exit. If an error is encountered, we skip past Exit, and execute the remaining lines.

    Hope taht helps,

    Mark

  5. #5
    VBAX Mentor
    Joined
    Aug 2008
    Posts
    323
    Location
    here is the corrected version
    [VBA]
    Sub ProcedureName()
    Dim MyFile As String
    MyFile = Dir$("\\a\b\c\d\e\*.xls")
    Do While MyFile <> ""
    On Error GoTo ErrorHandler
    Kill "\\a\b\c\d\e\" & MyFile
    MyFile = Dir$("\\a\b\c\d\e\*.xls")
    Loop
    Exit Sub
    ErrorHandler:
    MsgBox "Please ensure all files in the Reports folder are deleted. Unable to delete " & MyFile & " as readonly."
    If Workbooks(Str_WkbX).Sheets(Str_Wksht_X).ProtectContents = True Then Workbooks(Str_WkbX).Sheets(Str_Wksht_X).Unprotect Password:="54YY4F"
    Workbooks(Str_WkbX).Sheets(Str_Wksht_X).Range("I6").Value = "stopped"
    If Workbooks(Str_WkbX).Sheets(Str_Wksht_X).ProtectContents = False Then Workbooks(Str_WkbX).Sheets(Str_Wksht_X).Protect Password:="54YY4F"

    End Sub


    [/VBA]
    I am a Newbie, soon to be a Guru

  6. #6
    thanks all for your help

Posting Permissions

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