Andres has a picture in the Clipboard. He would like, for the selected cell, to place the picture into a comment. If the cell has no comment, then one would need to be created. If there is a comment already, then the picture would need to be added to it. Andres knows this would take a macro, but he's not sure how to work with comments in a macro to do what he needs.
Microsoft, in the latest Office 365 version, now refers to comments as notes. For the purposes of this tip, however, I'll continue to refer to them by the traditional comments name.
What Andres wants to do is nowhere near as easy as one might desire. The first problem is that there seems to be no way in VBA to use the Clipboard as the source of an image destined for a comment. It is possible to get around this by changing the source to be an image in a file—in other words, to have the macro allow the user to select an image file that is then placed in the comment.
Sub AddCommentPicture()
Dim PicChoice As Variant
If ActiveCell.Comment Is Nothing Then
ActiveCell.AddComment
End If
PicChoice = Application.GetOpenFilename("JPEGs *.jpg,*.jpg")
If PicChoice = False Then
MsgBox "No file was selected."
Else
ActiveCell.Comment.Shape.Fill.UserPicture PicChoice
ActiveCell.Comment.Shape.LockAspectRatio = True
End If
End Sub
The macro tests the active cell to see if it has a comment. If not, it will add one. It then displays an Open dialog box that shows only JPG files. (You can change the GetOpenFilename function's parameter to indicate what types of files should be displayed.) The file you pick is then assigned to the comment.
Note that the code does nothing to resize the image. You can, if desired, add the code necessary to do the resizing. You'll want to add that code directly after the line that locks the aspect ratio of the image, near the end of the macro.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (5489) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and 2021.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 365 applications with VBA programming. Written in clear terms and understandable language, the book includes systematic tutorials and contains both intermediate and advanced content for experienced VB developers. Designed to be comprehensive, the book addresses not just one Office application, but the entire Office suite. Check out Mastering VBA for Microsoft Office 365 today!
Pasting the contents of a single cell into a comment is rather easy. Pasting the contents of a range of cells is a ...
Discover MoreWant to automatically move the contents of a cell into a comment for that cell? It's easy enough to do by using the macro ...
Discover MoreNeed to find that misplaced comment in your worksheet? It's easy to do using the Find and Replace capabilities of Excel.
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2021-12-17 18:04:10
J. Woolley
For more discussion, see https://www.thespreadsheetguru.com/the-code-vault/vba-insert-image-into-cell-comment
2021-12-10 10:03:12
J. Woolley
@AndrésnSee https://sites.google.com/view/myexceltoolbox/Comment for continued discussion.
2021-12-09 12:05:45
Andrés
Hello Woolley,nnI forgot to mention, the file is not blocked.nnKRnnAndrés
2021-12-09 12:00:12
Andrés
Hello Woolley,nnUnfortunatelly, I'm not able to see the "new" ribbon after the add-on is installed.nThe add-on is also active, but not present somehow. I'm using Office 365.nnAny ideas?.nnThanks.nnKRnA. López
2021-12-08 10:16:18
J. Woolley
For more on this subject, see https://excelribbon.tips.net/T013280_Placing_a_Picture_in_a_Comment.htmlnnMy Excel Toolbox includes the following function that will add a background image to a comment:n=ImageInComment(ImageFile,[Target],[ScaleFactor],[RotateAngle],[NoAuthor])nSee https://sites.google.com/view/MyExcelToolbox/
2021-12-07 09:09:33
Andrés
Hello There,nnHere is the code that I use, I'm not able to check if there is a picture in the clipboard yet. Maybe someone can help me. BTY, the code is not mine unfortunatelly, credits to the author:nnOption ExplicitnSub PictureExport()nDim TempChart As String, Picture2Export As StringnDim PicWidth As Long, PicHeight As LongnDim ActSheet As Stringn nActSheet = ActiveSheet.NamennActiveSheet.PastenSelection.Name = "PrintScreen"nPicture2Export = Selection.Namenn n'Store the picture's height and width in a variablenWith Selectionn PicHeight = .ShapeRange.Heightn PicWidth = .ShapeRange.WidthnEnd Withn n'Add a temporary chart in sheet1nCharts.AddnActiveChart.Location Where:=xlLocationAsObject, Name:=ActSheetnSelection.Border.LineStyle = 0nTempChart = Selection.Name & " " & Split(ActiveChart.Name, " ")(2)n nWith ActiveSheetn 'Change the dimensions of the chart to suit your needn With .Shapes(TempChart)n .Width = PicWidthn .Height = PicHeightn End Withn n 'Copy the picturen .Shapes(Picture2Export).Copyn n 'Paste the picture in the chartn With ActiveChartn .ChartArea.Selectn .Pasten End Withn n 'Finally export the chartn .ChartObjects(1).Chart.Export Filename:=ThisWorkbook.Path & "\tmp.jpg", FilterName:="jpg"n .Shapes(TempChart).Cutn End WithnnActiveCell.AddCommentnActiveCell.Comment.Shape.Fill.UserPicture ThisWorkbook.Path & "\tmp.jpg"nActiveCell.Comment.Shape.Width = PicWidthnActiveCell.Comment.Shape.Height = PicHeightnActiveSheet.Shapes("PrintScreen").DeletenKill ThisWorkbook.Path & "\tmp.jpg"nApplication.ScreenUpdating = TruenEnd Sub
2021-09-11 07:30:23
Kurt Müller
@ J. WoolleynnOr you could put this formula in a worksheet cell:nnDoes not work fine (comma):nn=ImageInComment("D:/Test/Excel/0-Bilder/Erdbeere-10.jpg",H7)nnWorks fine (semicolon):nn=ImageInComment("D:/Test/Excel/0-Bilder/Erdbeere-10.jpg";H7)
2021-09-11 06:38:14
Kurt Müller
@ J. WoolleynnThank you very much.nnWorks fine:nnSub Image_In_Comment()nImageInComment "D:/Test/Excel/0-Bilder/Erdbeere-10.jpg", Range("H7")nEnd SubnnDoes not work fine (Compile error: Variable not defined)nnSub Image_In_Comment()nResult = ImageInComment("D:/Test/Excel/0-Bilder/Erdbeere-10.jpg", Range("H7")) '** Compile error: Variable not definednEnd SubnnWhat am I doing wrong?nnWorks fine in the Immediate Window:nnResult = ImageInComment("D:/Test/Excel/0-Bilder/Erdbeere-10.jpg", Range("H7"))
2021-09-06 10:06:55
J. Woolley
@Kurt MüllernAs described in my earlier comments, ImageInComment is a Function, not a Sub, so it can be used in a cell formula. If you want to use a Function in VBA instead of a cell formula and ignore the returned result, your example with a Call statement will work. The Application.Evaluate shortcut [H7] is OK, but Range("H7") would be better. Here are two other ways to use the Function in VBA:nImageInComment "D:/Test/Excel/0-Bilder/Erdbeere-10.jpg", Range("H7")nresult = ImageInComment("D:/Test/Excel/0-Bilder/Erdbeere-10.jpg", Range("H7"))nOr you could put this formula in a worksheet cell:n=ImageInComment("D:/Test/Excel/0-Bilder/Erdbeere-10.jpg",H7)
2021-09-05 13:50:48
Kurt Müller
@ J. WoolleynnI found the solution now:nnSub ImageInComment()nCall ImageInComment("D:/Test/Excel/0-Bilder/Erdbeere-10.jpg", [H7])nEnd SubnnI had to enter the above macro in a modul by myself.
2021-09-05 07:08:00
Kurt Müller
@ J. WoolleynnI can find Register ImageInComment function (Private Sub ImageInComment_Register()), Image in Target's unthreaded Comment (Public Function ImageInComment(ImageFile, [Target}, [ScaleFactor], [RotateAngle], [NoAuthor])) but I can not find a "Sub ImageInComment".nnWhat am I probably doing wrong?
2021-08-26 18:12:22
J. Woolley
For similar discussion, including the ImageInComment(FilePath,[Target]) function, see:nhttps://excelribbon.tips.net/T013280_Placing_a_Picture_in_a_Comment.html#comment-form-hd
2021-08-25 11:35:09
Kurt Müller
If a picture is pasted into a comment, the picture takes the size of the comment shape. After that the picture is already distorted.nnIf distortion of the picture shall be prevented, the following procedure is necessary:nn- After choosing the pic it is necessary to calculate the scale of the picturen- With the scale of the picture the size of the comment shape has to be changed to the scale of the picturen- If the picture is pasted into the comment shape - having the same scale as the picture - then the picture is not distortedn- Just now the aspect ratio of the comment shape has to be locked. It is not possible to lock the aspect ratio of the picture pasted into the comment shapennSub insert_userpicture_in_comments()nn'** Dimensionierung der Variablenn Dim rngZelle As Rangen Dim strFilename As Variantn Dim strFilter As Stringn Dim ScaleValue As Singlen Dim ScaleValue2 As Singlen Dim objPic As IPictureDispn Dim Source As String n Dim i As Integern n If ActiveSheet.Comments.Count = 0 Thenn MsgBox "No comments in entire sheet"n Exit Subn End Ifn n'Dateiauswahl filternn strFilter = "JPG Files (*.jpg), *.jpg" _n & ", GIF Files (*.gif), *.gif" _n & ", Bitmaps (*.bmp), *.bmp" _n & ", WMF Files (*.wmf), *.wmf"n n ' Dialogfenster zur Auswahl eines Bildes öffnenn strFilename = Application.GetOpenFilename(strFilter)n DoEventsnn' Wenn kein Bild ausgewählt wurde, Prozedur beendenn If strFilename = False Then GoTo LabelAnn Source = (CStr(strFilename))n DoEventsn n' Set objPic = LoadPicture(Bild)n Set objPic = LoadPicture(Source)n DoEventsn n With objPicn ScaleValue = .Width / .Heightn End Withn n If MsgBox(ScaleValue, vbOKCancel) = vbCancel Then GoTo LabelAnn Application.CutCopyMode = Falsen DoEventsnn Application.ScreenUpdating = Falsen Application.Calculation = xlCalculationManualnn'** Alle markierten rngZellen durchlaufennFor Each rngZelle In Selection.Cellsn With rngZellen If Not .Comment Is Nothing Thenn'Insert The Image and Resizen With .Comment.Shape n .LockAspectRatio = msoFalsen .Width = 150n' .Width = ScaleValue * .Heightn .Height = .Width / ScaleValuen .Fill.UserPicture strFilenamen DoEvents n .LockAspectRatio = msoTruen n ScaleValue2 = .Width / .Height n i = i + 1n Debug.Print i; rngZelle.Address; ScaleValue; ScaleValue2n n End Withn End Ifn End WithnNext rngZellennLabelA: n Application.Calculation = xlCalculationAutomaticn Application.ScreenUpdating = Truen On Error GoTo 0nEnd Sub
2020-04-12 13:40:37
J. Woolley
@RonmionFor example, to make the height of the comment shape 100 pixels, change the code after Else as follows:nn ActiveCell.Comment.Shape.Fill.UserPicture PicChoicen ActiveCell.Comment.Shape.LockAspectRatio = Truen ActiveCell.Comment.Shape.Height = 100nnWhen you use VBA to change the comment shape's height, its width will also change because aspect ratio is locked. In this case, the JPG image resizes to match the comment's shape because it was inserted as a background (Fill).nnNotice you can manually change the comment shape's height or width independently by dragging a side-handle, but dragging a corner-handle will change both height and width to maintain aspect ratio.
2020-04-11 14:00:28
Ronmio
What might the VBA code that sizes the JPG look like?
2020-04-11 08:15:42
Cliff Raymond
It never occurred to me that an image could be pasted into a comment, or why I would want to, but I still found this fascinating. Thanks!
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2026 Sharon Parq Associates, Inc.
Comments