使用VBA向单元格添加注释

时间:2017-08-09 21:09:19

标签: excel vba excel-vba excel-2013

有没有办法通过将鼠标悬停在单元格上来激活对单元格的评论?我有一系列的细胞,我想在每个单独的细胞上盘旋时从另一张纸上拉出相应的评论。悬停事件将从其他工作表中的相应单元格中提取注释。

评论是字符串值。基本上,我在Sheet 1中有一系列单元格,比方说A1:A5,当我将鼠标悬停在它们上面并从Sheet 2范围B1:B5拉出时,我需要弹出注释。我不会手动操作的原因是因为Sheet 2的内容每天都在变化。这就是我试图查看是否存在VBA解决方案的原因。

2 个答案:

答案 0 :(得分:0)

每次打开工作簿时,此代码都会刷新注释的内容。它基于目的地和来源的范围。请务必先为单元格区域添加注释。你不会因此而需要VBA。

Private Sub Workbook_Open()
    Dim ws As Worksheet
    Dim rg As Range
    Dim comment As String
    Dim i As Integer

i = 1
Set rg = Range("E1:E10") 'set range of where the comments will be seen
Set ws = Sheets("Sheet1")


For Each c In rg
comment = ws.Cells(i, 2).Value 'set location of comments you are grabbing from
c.comment.Text Text:=comment
i = i + 1
Next c
End Sub

答案 1 :(得分:0)

试试这段代码。

Sub test()
    Dim rngDB As Range, rngComent As Range
    Dim rng As Range
    Dim cm As Comment, i as integer
    Set rngComent = Sheets(1).Range("a1:a5")
    Set rngDB = Sheets(2).Range("b1:b5")

    For Each rng In rngComent
        i = i + 1
        If Not rng.Comment Is Nothing Then
            rng.Comment.Delete
        End If
        Set cm = rng.AddComment
        With cm
            .Visible = False
            .Text Text:=rngDB(i).Value
        End With
    Next rng

End Sub