這是一個通用的日誌系統,在這裏和我自己創建了一些人。我爲此感到自豪......我遇到了兩個問題......如果有人能夠幫助解決這個問題,那就太好了。我有一個代碼在Excel工作表中記錄使用情況,但是我得到一個錯誤和一個問題
下面是代碼:
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
這裏有兩個問題。
- 如果選擇了多於一次的單元格並嘗試寫入,則腳本出錯。
- 如果有人編輯單元格並讓它空白,它會顯示的
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
目標可能會返回一定範圍的單元格。作爲一個例子,你可以使用'在Target.Rows中爲每一行'循環Target中的行。你也可以找到'如果Target.Row = X','如果Target.Rows.Count = Y'和'If Not Intersect'條款有用(行/列可互換)。 – user3357963