Written by Allen Wyatt (last updated July 24, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and 2021
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 2021. You can find a version of this tip for the older menu interface of Excel here: Positioning a Graphic in a Macro.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
Excel is great at printing numbers on a piece of paper, but terrible at printing watermarks. This is apparently by ...
Discover MoreDon't like the color of the lines that Excel chose for your drawing object? It's easy to choose your own colors, as ...
Discover MoreExcel allows you to capture portions of your worksheet as a picture that you can then use in a variety of other ways. ...
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 © 2025 Sharon Parq Associates, Inc.
Comments