Hi Barb,
I like to write error information to a text file because users can rarely be
relied upon to accurately translate the info to the Administrator. Also it is
not uncommon for users to not tell you about them at all. By appending the
info to a txt file you can interrogate the file occasionally and see how
things are going. The following is a simplified demo. You can probably gather
other info like username etc and maybe others in this forum would like to
contribute suggestions on lots more good info that can be gathered and
written to the file.
Following sample code should give you the idea.
Option Explicit
'Dimensioning variables in the declarations area saves having to
'repeat this for every sub.
Dim strModuleName As String
Dim strSubname As String
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo errorCall
'Need to assign module name and sub name to variables
'for each sub.
strModuleName = "Sheet1"
strSubname = "Private Sub Worksheet_Change"
'Required code here
'Following line included to produce an error
'for testing purposes. (Sheet4 non-existant)
Sheets("Sheet4").Select
Exit Sub
errorCall:
Call Error_Routine(strModuleName, strSubname, Err.Number, Err.Description)
End Sub
Place the following code in a standard module:
Sub Error_Routine(ParamModule, _
ParamSubName, ParamErrNo, ParamErrDescript)
'Called from On Error and writes
'error info to "Error Log.txt"
Dim strNow As String
Dim strPath As String
Dim strFilename As String
Dim strFileToOpen As String
strNow = Format(Now(), "dd mmm yyyy hh:mm AMPM")
MsgBox "Date and Time: " & strNow & vbCrLf & _
"Module: " & ParamModule & vbCrLf & _
"Procedure: " & ParamSubName & vbCrLf & _
"Error Number: " & ParamErrNo & vbCrLf & _
"Error Description: " & ParamErrDescript & vbCrLf & _
vbCrLf & _
"Error information written to file Error Log.txt"
strPath = ThisWorkbook.Path
strFilename = "Error Log.txt"
strFileToOpen = strPath & "\" & strFilename
'If file exists open for append otherwise
'this command creates text file.
Open strFileToOpen For Append As #1
Print #1, " Date and Time: " & strNow
Print #1, " Module: " & ParamModule
Print #1, " Procedure: " & ParamSubName
Print #1, " Error Number: " & ParamErrNo
Print #1, "Error Description: " & ParamErrDescript
Print #1, " "
Print #1, "End of error message"
Print #1, "**********************************************"
Print #1, " "
Close #1
End Sub