Option Explicit
Private WithEvents cmbrs As CommandBars
Private Type POINTAPI
x As Long
y As Long
End Type
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If"]#If[/URL] VBA7 Then
Private Declare PtrSafe Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else"]#Else[/URL]
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End"]#End[/URL] If
Private Sub Workbook_Activate()
Call HookComments
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
Call HookComments
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call ClearCommentsTag
End Sub
Private Sub HookComments()
[COLOR=#008000][B]'Diplay A5 comment in cell K4 and D8 comment in cell C20[/B][/COLOR]
Call ClearCommentsTag
Call SetCommentPos([COLOR=#ff0000]TheComment:=Sheet1.Range("A5").Comment[/COLOR], [COLOR=#0000ff]AnchorCell:=Sheet1.Range("k4")[/COLOR])
Call SetCommentPos([COLOR=#ff0000]TheComment:=Sheet1.Range("D8").Commen[/COLOR]t, [COLOR=#0000ff]AnchorCell:=Sheet1.Range("C20")[/COLOR])
End Sub
Private Sub SetCommentPos(ByVal TheComment As Comment, ByVal AnchorCell As Range)
TheComment.Shape.AlternativeText = "@*!" & AnchorCell.Address
Set cmbrs = Application.CommandBars
End Sub
Private Sub ClearCommentsTag()
Dim oSh As Worksheet, oCom As Comment
For Each oSh In Me.Worksheets
If oSh.ProtectContents = False Then
For Each oCom In oSh.Comments
If Left(oCom.Shape.AlternativeText, 3) = "@*!" Then
oCom.Shape.AlternativeText = ""
End If
Next
End If
Next oSh
End Sub
Private Sub cmbrs_OnUpdate()
Dim tCurPos As POINTAPI, oRange As Range, sAnchor As String
On Error Resume Next
Application.DisplayCommentIndicator = xlCommentIndicatorOnly
GetCursorPos tCurPos
Set oRange = ActiveWindow.RangeFromPoint(tCurPos.x, tCurPos.y)
sAnchor = oRange.Comment.Shape.AlternativeText
If Left(sAnchor, 3) = "@*!" Then
With oRange.Comment.Shape
.Top = Range(Mid(sAnchor, 4, Len(sAnchor) - 3)).Top
.Left = Range(Mid(sAnchor, 4, Len(sAnchor) - 3)).Left
End With
oRange.Comment.Visible = True
End If
End Sub