我有一个代码来loggingExcel表中的使用情况,但是我得到一个错误和一个问题

这是一个通用的日志系统,在这里和我自己创造了一些人。 我很自豪…我遇到两个问题…如果有人可以帮助解决这个问题,那就太好了。

这里是代码:

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 

这是两个问题。

  1. 如果多于一次的单元格被选中,并试图写入,脚本错误了。
  2. 如果有人编辑一个单元格并将其留空,它将显示8/30/2012 1:45:01 PM Matthew Ridge changed cell $K$3 from Test to而不是8/30/2012 1:45:01 PM Matthew Ridge changed cell $K$3 from Test to Blank or Empty

马特

一些事情

  1. On Error Resume Next是不正确的处理。 除非和绝对必要,否则应该避免。
  2. 在使用Worksheet_Change事件时,最好closures事件,然后再打开事件以避免可能的无限循环。
  3. 如果您closures事件,则必须使用适当的error handling。
  4. 既然你正在存储的只是一个单元格在PreviousValue所以我假设你不希望代码在用户select多个单元格时运行?

我认为这是你正在尝试(未testing)?

 Option Explicit Dim PreviousValue Private Sub Worksheet_Change(ByVal Target As Range) Dim sLogFileName As String, nFileNum As Long, sLogMessage As String Dim NewVal On Error GoTo Whoa Application.EnableEvents = False sLogFileName = ThisWorkbook.Path & Application.PathSeparator & "Log.txt" If Not Target.Cells.Count > 1 Then If Target.Value <> PreviousValue Then If Len(Trim(Target.Value)) = 0 Then _ NewVal = "Blank" Else NewVal = Target.Value sLogMessage = Now & Application.UserName & _ " changed cell " & Target.Address & " from " & _ PreviousValue & " to " & NewVal nFileNum = FreeFile Open sLogFileName For Append As #nFileNum Print #nFileNum, sLogMessage Close #nFileNum End If End If LetsContinue: Application.EnableEvents = True Exit Sub Whoa: MsgBox Err.Description Resume LetsContinue End Sub Private Sub Worksheet_SelectionChange(ByVal Target As Range) PreviousValue = Target(1).Value End Sub 

这对我有效。 理想情况下,您可以在被跟踪的工作表上使用命名范围,您可以使用该范围限制跟踪仅在该范围内发生的更改。

 Const MAX_TRACKED_CELLS As Long = 50 Dim PreviousValues As Object Private Sub Worksheet_Change(ByVal Target As Range) Dim c As Range Dim haveDict As Boolean, val, addr haveDict = Not PreviousValues Is Nothing If Target.Cells.Count <= MAX_TRACKED_CELLS Then For Each c In Target.Cells addr = c.Address() If haveDict Then If PreviousValues.exists(addr) Then val = PreviousValues(addr) End If Else val = "{unknown}" End If If c.Value <> val Then Debug.Print "Changed:", addr, IIf(val = "", "Empty", val), _ " to ", IIf(c.Value = "", "Empty", c.Value) End If Next c End If End Sub Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim c As Range If PreviousValues Is Nothing Then Set PreviousValues = CreateObject("scripting.dictionary") Else PreviousValues.RemoveAll End If If Target.Cells.Count <= MAX_TRACKED_CELLS Then For Each c In Target.Cells PreviousValues.Add c.Address(), c.Value Next c End If End Sub