2016-10-17 5 views
0

Ich versuche, Auto-Kommentar-Protokoll-Code zu den Zellen, die geändert werden, aber ich möchte nur einen Kommentar hinzufügen, wenn die Zelle nicht leer ist, ich meine, wenn die Zelle bereits hat ein Inhalt aber mein Code, um Kommentare hinzufügen zu jeder Zelle, auch wenn es das erste Mal, dass ich etwas zu der neuen Zelle schreibe könnten Sie mir bitte über dieses denkenVBA, Kommentar nur hinzufügen, wenn die Zelle nicht leer ist

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 

Antwort

0

helfen i i meine eigene Antwort gefunden Der folgende Code funktioniert und fügt nur Kommentare hinzu, wenn die Zelle nicht leer ist. So ist es einfach, Änderungen an jeder Zelle zu verfolgen.

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 
Verwandte Themen