使用 VBA 向单元格添加注释

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45600673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 13:04:29  来源:igfitidea点击:

Add comments to cells using VBA

excelvbaexcel-vbaexcel-2013

提问by studentofarkad

Is there a way to activate a comment on a cell by hovering over it? I have a range of cells that I would like to pull respective comments from another sheet when hovered over each individual cell. The hover event would pull the comments from their respective cells in the other sheet.

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

The comments are of string value. Basically, I have a range of cells in Sheet 1, let's say A1:A5 and I need comments to pop-up when I hover over them and pull from Sheet 2 range B1:B5. The reason why I won't do it manually is because the contents of Sheet 2 change every day. That is why I am trying to see if there is a VBA solution.

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

回答by jsotola

hovering over any cell, that contains a comment, shows that cell's comment

将鼠标悬停在包含评论的任何单元格上,会显示该单元格的评论

this is how you add a comment to a cell and how you update the comment text

这是向单元格添加评论以及更新评论文本的方式

Sub aaa()
    With Range("E6")
        If Not .Comment Is Nothing Then .Comment.Delete
        .AddComment "this is a comment"
        .Comment.Text "abc123" 'No need the assignment sign "=" after .Comment.Text             
    End With
End Sub

回答by TJYen

This code will refresh the contents of the comments every time you open the workbook. It is based on ranges of both destination as well as source. Make sure to first add a comment for the cell range first. You won't need VBA for that.

每次打开工作簿时,此代码都会刷新注释的内容。它基于目标和源的范围。确保先为单元格区域添加注释。你不需要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

回答by Dy.Lee

Try this code.

试试这个代码。

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