When you write code, you may want your application to totally ignore any errors that occur in a certain procedure. You can do this by entering
On Error Resume Next
at the start point, where you want it to ignore errors. To re enable the error messages in that procedure type
On Error Goto 0
For example, the following code attempts to delete Test.txt from drive C:. The first attempt, errors are ignored, but the second time, an error occurs when the procedure attempts to delete the file (providing the file does not exist).
Sub Command1_Click()
On Error Resume Next ' Ignore all errors from this point
Kill "C:\test.txt"
' An error occurs if the file does not exist, but it is ignored
' The error number and message is printed to the debug window
Debug.Print Err & " : " & Error
On Error GoTo 0 ' Errors from this point onwards will cause a break
Kill "C:\test.txt"
' An error occurs, and a message is displayed. Code execution stops.
End Sub
Err returns the last error number that occurred. Error returns the last error message. Kill deletes the specified file
Comments