2016-10-17 11 views
0

変更されたセルに自動コメントログコードを追加しようとしていますが、セルが空でない場合にのみコメントを追加します。コンテンツ しかし、私のコードは、私が新しいセル に何かを書いています、その最初の時間はあなたが私は私自身の答えを見つけたと思う。このvba、セルが空でない場合にのみコメントを追加する

Private Sub Worksheet_Change(ByVal Target As Range) 
Dim singlecell As Range 

If Target.Cells.CountLarge > 100000 Then Exit Sub 

For Each singlecell In Target 

If singlecell.Comment Is Nothing And Target.Value <> "" Then 

singlecell.AddComment Now & " - " _ 
& "new value: " _ 
& singlecell.Value & " - " _ 
& Environ("username") & " -" _ 
& "changed the value from a NULL value." 
ElseIf Not singlecell.Comment Is Nothing And Target.Value <> "" Then 
Target.Comment.Text _ 
vbNewLine & Now & " - " _ 
& "value changed to: " _ 
& Target.Value & " - by: " _ 
& Environ("username") & " -" _ 
, Len(Target.Comment.Text) + 1 _ 
, False 


ElseIf singlecell.Comment Is Nothing And Target.Value = 0 Then 
Exit Sub 



End If 



singlecell.Comment.Shape.TextFrame.AutoSize = True 

Next singlecell 
End Sub 

答えて

0

について私を助けてくださいできたとしても、すべてのセルにコメントを追加して 以下のコードは動作し、セルが空でない場合にのみコメントを追加します。そのため、これはどのセルに行った変更も容易に追跡できます。

Private Sub Worksheet_Change(ByVal Target As Range) 
Application.ScreenUpdating = False 
Application.Calculation = xlCalculationManual 
Application.EnableEvents = False 
Application.DisplayAlerts = False 
If ActiveCell.Value = vbNullString Then 
Exit Sub 
Else 
If Target.Comment Is Nothing And Target.CountLarge < 2 Then 

Target.AddComment Now & " - " _ 
& "prev value = " _ 
& ActiveCell.Value & " - " _ 
& "new value: " _ 
& Target.Value & " - " _ 
& Environ("username") & " -" _ 
& "changed the value from a NULL value." 

ElseIf Not Target.Comment Is Nothing Then 
Target.Comment.Text _ 
vbNewLine & Now & " - " _ 
& "value changed to: " _ 
& Target.Value & " - by: " _ 
& Environ("username") & " -" _ 
, Len(Target.Comment.Text) + 1 _ 
, False 

Exit Sub 


End If 

End If 
If Target.CountLarge < 2 Then 
Target.Comment.Shape.TextFrame.AutoSize = True 
End If 
Application.ScreenUpdating = True 
Application.Calculation = xlCalculationAutomatic 
Application.EnableEvents = True 
Application.DisplayAlerts = True 
End Sub 
関連する問題