如何使用VBA中的单元格属性添加注释

时间:2017-11-09 21:32:14

标签: excel-vba vba excel

如何使用单元格属性添加注释,就像我需要向Cells(1,i)添加注释一样。

我知道如何使用Range属性添加注释(Range(" A1")。AddComment),但我想通过cells属性添加。请帮助excel vba。

Sub t()
Dim headers() As Variant
Dim i As Integer

headers() = Array("FIRST", "Second", "Third")
For i = 1 To 3
m = Cells(1, i).Value
If Cells(1, i).Value <> headers(i - 1) Then
Cells(1, i).Interior.Color = vbYellow
Cells(1, i).AddComment 'I need to add some text here"

MsgBox ("Not equal")
End If
Next i

End Sub

2 个答案:

答案 0 :(得分:1)

从您刚刚发布的代码中,您所遗漏的只是AddComment方法之后的字符串。 修改:您可能已在某些目标单元格中​​发表评论。我已经添加了对ClearComments的调用。

Sub t()
    Dim headers() As Variant
    Dim i As Integer
    Dim m As Variant

    headers() = Array("FIRST", "Second", "Third")

    For i = 1 To 3
        m = Cells(1, i).Value
        If Cells(1, i).Value <> headers(i - 1) Then
            Cells(1, i).Interior.Color = vbYellow
            Cells(1, i).ClearComments
            Cells(1, i).AddComment "Hello World" '<===== Here
            MsgBox ("Not equal")
        End If
    Next i
End Sub

答案 1 :(得分:0)

它可能与谁有关,问题是如果您已经有评论,则必须先删除它,然后才能添加更多评论。只需在添加注释之前添加以下内容:

**Dim rng As Range
Set rng = Cells(row_num, col_num)
If Not (rng.comment Is Nothing) Then
    rng.comment.Delete
End If**

rng.AddComment (in_comment)