vba,仅在单元格不为空时添加注释

时间:2016-10-17 13:57:27

标签: vba comments target worksheet

我正在尝试将自动注释日志代码添加到已更改的单元格但我想仅在单元格不为空时添加注释,我的意思是如果单元格已经有内容 但是我的代码正在为每个单元格添加注释,即使它是我第一次向新单元格写入内容 你能帮我解决一下吗

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

2 个答案:

答案 0 :(得分:0)

我想我找到了自己的答案 以下代码有效,只有在单元格不为空时才添加注释。因此,它可以轻松跟踪对任何单元格所做的更改。

{{1}}

答案 1 :(得分:0)

    On Error Resume Next
    S_Cell.AddComment
    If Err.Number <> 0 Then
        S_Cell.Comment.Delete
        S_Cell.AddComment
    End If