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.

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


3

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:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

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.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Saving a Workbook Using Passwords

One of the security features built-in to Excel is the ability to save a workbook using a password that limits access to ...

Discover More

Removing Entire Paragraphs from Your Document

If you need to get rid of a lot of paragraphs in a document, it's easy to do as long as the document relies on styles for ...

Discover More

Adding Quoted Words to an Index

It is not unusual to need to convert one notation in a document into another entirely different notation. For instance, ...

Discover More

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!

More ExcelTips (ribbon)

Automatically Displaying Thumbnails of a Graphic File

If you want to include a large number of images in your worksheet, you may also want a way to automatically add those ...

Discover More

Watermarks in Excel

Excel is great at printing numbers on a piece of paper, but terrible at printing watermarks. This is apparently by ...

Discover More

Inserting a Picture in Your Worksheet

Worksheets can contain more than just text and numbers. Here's the low-down on the different types of pictures you can ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 2 + 2?

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}]


This Site

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.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.