This is a universal log system, that a few people here and myself have created. I’m rather proud of it… I am running into two issues… if someone can help with the sollution it be great.
Here is the code:
Option Explicit
Dim PreviousValue
Private Sub Worksheet_Change(ByVal Target As Range)
Dim sLogFileName As String, nFileNum As Long, sLogMessage As String
sLogFileName = ThisWorkbook.path & Application.PathSeparator & "Log.txt"
On Error Resume Next ' Turn on error handling
If Target.Value <> PreviousValue Then
' Check if we have an error
If Err.Number = 13 Then
PreviousValue = 0
End If
' Turn off error handling
On Error GoTo 0
sLogMessage = Now & Application.UserName & " changed cell " & Target.Address _
& " from " & PreviousValue & " to " & Target.Value
nFileNum = FreeFile ' next file number
Open sLogFileName For Append As #nFileNum ' create the file if it doesn't exist
Print #nFileNum, sLogMessage ' append information
Close #nFileNum ' close the file
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
PreviousValue = Target(1).Value
End Sub
Here are the two issues.
- If more than once cell is selected, and attempted to be written to, the script errors out.
- If someone edits a cell and leaves it blank, it will show
8/30/2012 1:45:01 PM Matthew Ridge changed cell $K$3 from Test toinstead of8/30/2012 1:45:01 PM Matthew Ridge changed cell $K$3 from Test to Blank or Empty
Matt
Few Things
On Error Resume Nextis not proper handling. It should be avoided unless and until it is absolutely necessary.Worksheet_Changeevent, it is better to switch off events and then turn them back on at the end to avoid possible endless loop.PreviousValueso I am assuming that you do not want the code to run when the user selects multiple cells?I think this is what you are trying (UNTESTED)?