9

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.

4
  • you can rightclick a cell and add a comment.... but to set the text dynamically will need a bit of vba... please show what you have tried so far Commented Aug 9, 2017 at 21:13
  • Are the comments of string value? and are the comments being pulled from cells or comment boxes in the other sheet?
    – TJYen
    Commented Aug 9, 2017 at 21:16
  • 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. Commented Aug 9, 2017 at 21:35
  • There is no built-in hover event (try seeing what happens when you use the macro recorder to hover over a commented cell - the comment shows in Excel's interface but nothing gets added to the recorded macro). So, you will likely need a macro that looks pairwise at cells in your source and target ranges and sets the Comment property of the target cell to be that of the source cell.
    – DMM
    Commented Aug 10, 2017 at 11:32

5 Answers 5

7

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
1
  • in Excel VB worked this: ` Set reg1 = Range(("E6") Dim txt1 As String txt1 = Trim(Range(letter + strRow).FormulaR1C1) If Not reg1.Comment Is Nothing Then reg1.Comment.Delete End If Set cm = reg1.AddComment cm.Visible = False reg1.Value = txt1 `
    – Andrew
    Commented Dec 29, 2023 at 13:36
2

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
2
  • Unfortunately, I get "variable" not defined as the error message. Commented Aug 11, 2017 at 21:23
  • @studentofarkad, i as integer missed.
    – Dy.Lee
    Commented Aug 12, 2017 at 5:54
2

A less bulky "All-in-One" solution:

Sub comment(rg As Range, Optional txt As String = "")
  If rg.comment Is Nothing Then
    If txt <> "" Then rg.addComment txt
  Else
    If txt = "" Then rg.comment.Delete Else rg.comment.text txt
  End If
End Sub

Usage:

Using cell [a1] as an example ...but shortcut notation like [a1]) should generally be avoided except for testing, etc

  • Add or change comment: comment [a1], "This is my comment!"
  • Delete existing comment: comment [a1], "" or simply comment [a1]

Related stuff:

  • set comment box size:[a1].comment.Shape.Width = 15 and[a1].comment.Shape.Height = 15
  • set the box position: [a1].comment.Shape.Left=10 and [a1].comment.Shape.Top=10
  • change background box color: [a1].comment.Shape.Fill.ForeColor.RGB = vbGreen

Nowadays I think that comments (or "notes", as they're now called) are hidden by default.

  • Always show all comments: Application.DisplayCommentIndicator=1

  • Show when mouse hovers over cell: Application.DisplayCommentIndicator=-1

  • Disable comments (hide red indicator): Application.DisplayCommentIndicator=0

  • show/hide individual comments like [a1].comment.visible=true, etc.

  • get comment text: a=[a1].comment.text

0

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.

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
  • which Range("E1:E10") is on an undetermined worksheet
    – jsotola
    Commented Aug 10, 2017 at 21:17
-1

I've discovered that if the sheet cell is previously formatted and contains data the VBA Add Comments routines may not work. Also, you have to refer to the cell in the "Range" ("A1") format, not the "Cells" (Row Number, Column Number) format. The following short sub worked for me (utilize prior to program formatting/adding data to cell):

Sub Mod01AddComment()

Dim wb As Workbook
Set wb = ThisWorkbook
Dim WkSheet As Worksheet
Set WkSheet = wb.Sheets("Sheet1")

Dim CellID As Range

Set CellID = WkSheet.Cells(RowNum, ColNum)
` ( or, Set CellID = WkSheet.Range("A1") )

CellID.Clear

CellID.AddComment
CellID.Comment.Visible = False
CellID.Comment.Text Text:="Comment Text"

End Sub

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.