How do I copy cell text to comments and vise versa

senthile

New Member
Joined
Jan 24, 2014
Messages
10
How do I copy cell text to comments in excel 2010 and vise versa (i.e. from comments to cell)
 
Code:
Sub swap()
This is the title of the macro
Code:
If ActiveCell.Comment Is Nothing Then
ActiveCell is the cell you have highlighted when yourun the macro. So this if statement checks if your current cell has no comment.
Code:
x = ""
ActiveCell.AddComment
If there was no comment, this code executes. x is a variable I chose to store the comment value. So if there was no comment, I define x as blank (""). Then I create a blank comment.
Code:
Else
x = ActiveCell.Comment.Text
End If
This else code executes if the if statement was false, so in this case if there really was a comment. In that case, we define x as the comment's text.

Code:
y = ActiveCell.Value
y is another variable, and here I store the cell value to it.

Code:
If y = Empty Then
'ActiveCell.Comment.Delete
If the cell was empty, then I delete the comment.

Code:
Else
'ActiveCell.Comment.Text = y
End If
Otherwise, I set the comment text to y. Remember that we previously defined y as the cell contents.

Code:
ActiveCell.Value = x
End Sub
Last, I set the cell value to x, which was the comment text. End Sub ends the macro.

Remember that an apostrophe (') mutes a line. So when I gave you the two variants of the macro, I just muted the portions that performed the actions you didn't want.

Keep on Googling. Macros can do just about anything, and it's a kick learning about them.
 
Upvote 0

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Hi Bruderbell,

Need a another help, when i try to use the below given VBA quote (Macro) to copy the date (Eg : 15-May-2013), i am getting an error as "Run-time error '1004': Aplication-defined or object-defined error", when i press "Debug" it comes to "ActiveCell.Comment.Text y" with yellow highlight. can you help me to fix this.
Note : I will be using this macro to copy the text as well as data alone many times.

Sub swap()
If ActiveCell.Comment Is Nothing Then
x = ""
ActiveCell.AddComment
Else
x = ActiveCell.Comment.Text
End If
y = ActiveCell.Value
If y = Empty Then
ActiveCell.Comment.Delete
Else
ActiveCell.Comment.Text y
End If
'ActiveCell.Value = x
End Sub
 
Upvote 0
Indeed. The macro was pickup up that date, and seeing it was a date, it wasn't treating it as a string. This is working for me:
Code:
Sub swap()
Dim y As String, x As String
If ActiveCell.Comment Is Nothing Then
x = ""
ActiveCell.AddComment
Else
x = ActiveCell.Comment.Text
End If
y = ActiveCell.Value
If y = Empty Then
ActiveCell.Comment.Delete
Else
ActiveCell.Comment.Text y
End If
'ActiveCell.Value = x
End Sub
 
Upvote 0
Indeed. The macro was pickup up that date, and seeing it was a date, it wasn't treating it as a string. This is working for me:
Code:
Sub swap()
Dim y As String, x As String
If ActiveCell.Comment Is Nothing Then
x = ""
ActiveCell.AddComment
Else
x = ActiveCell.Comment.Text
End If
y = ActiveCell.Value
If y = Empty Then
ActiveCell.Comment.Delete
Else
ActiveCell.Comment.Text y
End If
'ActiveCell.Value = x
End Sub

No words to say... many thanks.... for your instant reply... I works....
 
Upvote 0
- Just found that this is working for each cell...
- if i choose multiple cells, still macro runs on 1st cell? are there any possibility to run this macro on selected multiple cell?
 
Upvote 0
Hi semthile

try below code. I just modified what you had before:

Code:
Sub swap()
Dim rng As Range, cell As Range
Dim y As String, x As String

Set rng = Selection

For Each cell In rng

    If cell.Comment Is Nothing Then
    x = ""
    cell.AddComment
    Else
    x = cell.Comment.Text
    End If
    y = cell.Value
    If y = Empty Then
    cell.Comment.Delete
    Else
    cell.Comment.Text y
    End If
    'Cell.Value = x

Next cell
End Sub
 
Upvote 0
It is working...
Similarly can share me the revised below given macro to copy multiple selected comments to multiple selected cells?

Sub swap()
If ActiveCell.Comment Is Nothing Then
x = ""
ActiveCell.AddComment
Else
x = ActiveCell.Comment.Text
End If
y = ActiveCell.Value
If y = Empty Then
'ActiveCell.Comment.Delete
Else
'ActiveCell.Comment.Text y
End If
ActiveCell.Value = x
End Sub
 
Upvote 0
Code:
Sub swap()
Dim rng As Range, cell As Range
Dim y As String, x As String

Set rng = Selection

For Each cell In rng

If cell.Comment Is Nothing Then
x = ""
cell.AddComment
Else
x = cell.Comment.Text
End If
y = cell.Value
If y = Empty Then
'Cell.Comment.Delete
Else
'Cell.Comment.Text y
End If
cell.Value = x

Next cell
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,392
Messages
6,119,257
Members
448,880
Latest member
aveternik

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top