Hi - For selected cells, i need VBA code which copies cell values and paste it as comment. If existing cell comment is there, it should not overwrite it
Paste selected cell value as comment without overwriting existing comment
- rkvk
- Thread is marked as Resolved.
Please note that on 14th December 2023 users will experience an expected outage whilst we make upgrades to our network. We anticipate this process may take a couple of hours and so we apologise in advance for any inconvenience.
-
-
-
Try this
Code
Display More'--------------------------------------------------------------------------------------- ' Module : ' DateTime : 16/12/2005 18:06 ' Author : Roy Cox (royUK) ' Website : [url]www.excel-it.com[/url] ' Purpose : place comment text in adjacent cell '--------------------------------------------------------------------------------------- Sub GetCommentText() Dim rRng As Range, rCl As Range Dim sCmt As String On Error Resume Next Set rRng = Intersect(Selection, _ Selection.SpecialCells(xlCellTypeComments)) On Error GoTo 0 If rRng Is Nothing Then MsgBox "No comments within selection" Exit Sub End If For Each rCl In rRng With rCl sCmt = .Comment.Text & vbNewLine & rCl.Value .Comment.Delete .AddComment (sCmt) ''///format Note(Comment) With rCl.Comment .Shape.Shadow.Visible = msoFalse .Visible = False .Shape.TextFrame.AutoSize = True End With End With Next rCl End Sub
-
I've re-read the question and I think this is what you mean.
Code
Display MoreSub GetCommentText() Dim rRng As Range, rCl As Range Dim sCmt As String For Each rCl In Selection If rCl.Comment Is Nothing And rCl.Text <> "" Then sCmt = rCl.Value rCl.AddComment (sCmt) ''///format Note(Comment) With rCl.Comment .Shape.Shadow.Visible = msoFalse .Visible = False .Shape.TextFrame.AutoSize = True End With End If Next rCl End Sub
-
First code is helpful..only scenario it doesn't work is
1) if there is no existing cell comment then it doesn't add cell value in the comment. example if cell A1 has calculated value as 45 but currently no comment, macro doesnt do anything. I need this macro also to add 45 as cell comment.
2) if cell comment can be added on top of existing comment. currently it adds below existing comment.
thanks so much for help.
-
Try this
Code
Display MoreSub GetCommentText() Dim rCl As Range Dim sCmt As String For Each rCl In Selection.Cells If rCl.Text <> "" Then With rCl If .Comment Is Nothing Then sCmt = rCl.Value Else: sCmt = .Value & vbNewLine & .Comment.Text .Comment.Delete End If Debug.Print sCmt .AddComment (sCmt) ''///format Note(Comment) With rCl.Comment .Shape.Shadow.Visible = msoFalse .Visible = False .Shape.TextFrame.AutoSize = True End With End With End If Next rCl End Sub
-
Participate now!
Don’t have an account yet? Register yourself now and be a part of our community!