PDA

View Full Version : Solved: error handling error



khalid79m
12-18-2009, 09:44 AM
:banghead: [CODE]Dim MyFile As String
MyFile = Dir$("\\a\b\c\d\e\*.xls (file://\\a\b\c\d\e\*.xls)")
Do While MyFile <> ""
On Error GoTo ErrorHandler
Kill "(

khalid79m
12-18-2009, 10:07 AM
Dim MyFile As String
MyFile = Dir$("\\a\b\c\d\e\*.xls (file://a/b/c/d/e/*.xls)")Do While MyFile <> ""
On Error GoTo ErrorHandler
Kill "\\a\b\c\d\e\ (file://a/b/c/d/e/*.xls)" & MyFile
MyFile = Dir$("\\a\b\c\d\e\*.xls (file://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


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

lucas
12-18-2009, 10:13 AM
If you comment out

On Error Goto ErrorHandler

and run it, what does the debugger tell you? What error do you receive?

GTO
12-18-2009, 05:40 PM
...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

nepotist
12-19-2009, 09:13 AM
here is the corrected version

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

khalid79m
01-10-2010, 06:23 AM
thanks all for your help