Excel VBA注释更新

时间:2018-07-08 06:15:32

标签: excel vba excel-vba

我正在尝试编写代码以检查注释是否存在,然后对其进行更新(只需将新文本附加到注释的末尾),我不想删除注释数据。我的新文本,用于从单元格值中获取注释。我的代码总是删除旧注释,然后添加新注释。

                            comm = user & ": " & ActiveCell.Value
                            With ActiveCell
                            If ActiveCell.Column = 10 Then
                                ActiveCell.Interior.ColorIndex = 27
                            Else
                                ActiveCell.Interior.ColorIndex = 0
                            End If
                            With ActiveCell.Borders
                                .LineStyle = xlContinuous 'Setting style of border line
                                .Weight = xlThin 'Setting weight of border line
                                .ColorIndex = xlAutomatic 'Setting colour of border line
                            End With
                            If .Comment Is Nothing Then
                                .AddComment
                                .Comment.Text Text:=comm
                                .Comment.Shape.TextFrame.AutoSize = True
                            Else
                                comm = .Comment.Text
                                Txt = comm & vbNewLine & Txt
                                .Comment.Text Text:=Txt
                                .Comment.Shape.TextFrame.AutoSize = True
                            End If
                        End With

1 个答案:

答案 0 :(得分:0)

无需使它变得如此复杂。请注意,Comment.Text是方法,而不是属性。

If .Comment Is Nothing Then
    .AddComment comm
Else
    .Comment.Text comm & vbNewLine, 1, False ' according to MSDN this will pre-pend the text, not overwrite - tested and works
    .Comment.Text .Comment.Text & vbNewLine & comm ' Alternative approach, this should overwrite with the new combined text
End If
.Comment.Shape.TextFrame.AutoSize = True ' because we know a comment exists after running this code