How can I create an error log?

G

Guest

How would I create an error log that would store log data to a cell range or
a .txt file, to include: Error number, error name/description, line of
suspected erronious code and if possible some basic information about what
they did to create that error?

I am having some intermittent errors that occur in my VB code on different
machines... Because no one else at my office has any idea what I'm doing
they disregard the error and move on. Then all I get is the extremely useful
error report from them: "Your excel file doesn't work!" which is very hard to
debug!

Any help or suggestions on implementing this would be appreciated.

Thank you!

~Josh
 
R

RB Smissaert

See if something like this suits you:


Sub test()

Dim bt As Byte
Dim btSum As Byte
Dim arrErrorData(1 To 6)

10 On Error GoTo ERROROUT

20 For bt = 1 To 300
30 btSum = btSum + bt
40 Next

50 Exit Sub
ERROROUT:

60 arrErrorData(1) = Format(Now, "dd/mmm/yyyy hh:mm")
70 arrErrorData(2) = "Module: Module1"
80 arrErrorData(3) = "Procedure: Sub test()"
90 arrErrorData(4) = "Error line: " & Erl
100 arrErrorData(5) = "Error number: " & Err.Number
110 arrErrorData(6) = Err.Description

120 Save1DArrayToTextAppend "C:\ErrorqqLog.txt", arrErrorData

End Sub


Sub Save1DArrayToTextAppend(txtFile As String, _
arr As Variant, _
Optional LB As Long = -1, _
Optional UB As Long = -1)

Dim r As Long
Dim hFile As Long

If LB = -1 Then
LB = LBound(arr)
End If

If UB = -1 Then
UB = UBound(arr)
End If

hFile = FreeFile

Open txtFile For Append As hFile

For r = LB To UB
If r = UB Then
Write #hFile, arr(r)
Else
Write #hFile, arr(r);
End If
Next

Close #hFile

End Sub


Just copy and paste in a module and run the Sub test.


RBS
 
G

Guest

Amazing!!! I'm really impressed! Thank you so much! I will just have the
log file saved to my network drive so I can access it from my computer and do
"check ups" on my program!!
 
R

RB Smissaert

No trouble. 2 further tips:
If you have to add this to a lot of procedures then you could make a central
error writing Sub with a few arguments and save yourself a lot of typing.
Secondly get MZ-Tools as that can quickly add the error lines for you + has
lots of other good things:
http://www.mztools.com/index.htm

RBS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top