Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Positioning a Graphic in a Macro.
Written by Allen Wyatt (last updated July 24, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365
Felix is writing a macro to add a graphic to a worksheet. He needs to position the graphic relative to the top-left corner of a particular cell. He wonders how he can place the graphic, within the macro code, so it is just to the right and beneath the upper-left corner of a given cell.
This task is relatively easy to do if you realize that each cell in a worksheet has both a Top and Left property that defines the location of both the top and left edges of the cell. You can adjust those values, slightly, to get the offset that you want, in this manner:
Dim rCell As Range Set rCell = Range("A2") With ActiveSheet.Shapes("Picture 1") .Top = rCell.Top + 5 .Left = rCell.Left + 3 End With
Note that after this code is executed the graphic (defined by the name Picture 1) is placed just below the top edge of cell A2 and just to the right of its left edge.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9726) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Positioning a Graphic in a Macro.
Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!
Excel allows you to set up graphics so that they are associated with cells and even stay with the cells when the cells ...
Discover MoreWant to "see through" an image you place on a worksheet? You can do so by using the steps in this tip.
Discover MoreExcel allows you to add comments to individual cells in a worksheet, but what if you want to add comments to graphics? ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2022-07-26 11:26:29
J. Woolley
The ListShapes function described in my previous comment has been overhauled to correctly process grouped shapes and linked formulas.
See https://excelribbon.tips.net/T009264_Finding_and_Replacing_in_Text_Boxes.html
The following columns are now returned for each shape:
Range, Group, Type, Shape Name, Link Formula, Macro Name, Hyperink Address
An option has been added to list each shape's text in a final column labeled Shape Text:
=ListShapes([AllSheets],[SkipHeader],[IncludeText])
The SpillArray function has also been updated to correct a potential error when sheets are inserted or rearranged.
See https://sites.google.com/view/MyExcelToolbox/
2022-01-18 11:29:13
J. Woolley
@Michael Simons
This macro will put an "X" in each picture/shape's top-left cell, then delete the picture/shape:
Sub ShapeX()
For Each s In ActiveSheet.Shapes
s.TopLeftCell.Value = "X"
s.Delete
Next s
End Sub
My Excel Toolbox includes this dynamic array function:
=ListShapes([AllSheets],[SkipHeader])
This will return the following columns for each shape:
Range, Type, Shape Name, Link Formula, Macro Name, Hyperink Address
In older versions of Excel you can use it with the SpillArray function like this:
=SpillArray(ListShapes([AllSheets],[SkipHeader]))
See https://sites.google.com/view/MyExcelToolbox/
2022-01-17 16:29:39
Michael Simons
Related to Positioning a Graphic in a Macro. Would there be a way of determining the position of a pictures relative to a cell address in Excel VBA? The reason for this, many SAP reports have a graphic that is a hyperlink in SAP, but when exported to Excel. I would like to be able to put a text value (say "X") in the cell under the graphic that will then make the work sheet filterable. If there were only a dozen rows it would be easy. But when there are hundreds or thousands it become s impractical. Was thinking - loop through all pictures, underlying cell value = "X", delete picture. This is a screen shot.
{{fig}]
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 © 2024 Sharon Parq Associates, Inc.
Comments