[CODE]Dim MyFile As String
MyFile = Dir$("\\a\b\c\d\e\*.xls")
Do While MyFile <> ""
On Error GoTo ErrorHandler
Kill "(
[CODE]Dim MyFile As String
MyFile = Dir$("\\a\b\c\d\e\*.xls")
Do While MyFile <> ""
On Error GoTo ErrorHandler
Kill "(
[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
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
Hi there,Originally Posted by khalid79m
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
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
thanks all for your help