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 Excel in Office 365.
Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!
If you frequently add comments to cells in a worksheet, Excel provides a variety of tools you can use to manage those ...
Discover MoreNeed to find that misplaced comment in your worksheet? It's easy to do using the Find and Replace capabilities of Excel.
Discover MoreNeed to copy whatever is in a comment into a cell on your worksheet? If you have lots of comments, manually doing this ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2020-04-12 13:40:37
J. Woolley
@Ronmio
For example, to make the height of the comment shape 100 pixels, change the code after Else as follows:
ActiveCell.Comment.Shape.Fill.UserPicture PicChoice
ActiveCell.Comment.Shape.LockAspectRatio = True
ActiveCell.Comment.Shape.Height = 100
When 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).
Notice 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 © 2021 Sharon Parq Associates, Inc.
Comments