How to Insert, Replace, and Center an Image in an Excel Cell Using VBA
Question details
The user needs a VBA solution to dynamically select an image, delete an existing picture in a specific cell (like B1), and then insert, resize, and properly center the new image within that cell.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Automating the process of updating and positioning images inside specific spreadsheet cells without the images snapping to the wrong location.
- Observed behavior
- Standard VBA codes often fail to replace the old image or position the new one incorrectly (e.g., appearing near cell C5 instead of B1) due to unreliable picture naming or incorrect cell anchoring.
Ensure the Developer tab is enabled in your Excel ribbon and that you save your workbook as a Macro-Enabled Workbook (.xlsm) to prevent losing your VBA code.
Use Target Cell Properties for Precise Image Positioning
Write a VBA script that iterates through shapes to identify and delete the old image, prompts for a new file, and accurately calculates the center position using the target cell's Top, Left, Width, and Height properties.
Relying on generic picture names (like 'Picture 1') or standard insertion methods can cause images to appear in unintended locations. The most reliable method is to anchor and size the picture by mathematically calculating its position relative to the specific dimensions of the target cell.
Press ALT + F11 to open the Visual Basic for Applications editor. Go to Insert > Module to create a new blank module for your macro.
Define your target cell (e.g., Range("B1")). Loop through ActiveSheet.Shapes and check if the shape's TopLeftCell.Address matches the target cell. If it does, execute the Shape.Delete command.
Use the Application.GetOpenFilename method to open a file picker, allowing the user to select the new image file they wish to insert.
Use ActiveSheet.Pictures.Insert to add the selected file. Set the picture's ShapeRange.LockAspectRatio to msoTrue, then adjust its Width or Height so it is slightly smaller than the target cell.
Set the image's Left property to: TargetCell.Left + (TargetCell.Width - Image.Width) / 2. Set the Top property to: TargetCell.Top + (TargetCell.Height - Image.Height) / 2. This perfectly centers the image regardless of previous anchors.

Easily Manage Spreadsheets and Images with WPS Office
Troubleshooting VBA macros for precise image placement in Microsoft Excel can be frustrating and time-consuming. WPS Office provides a free, lightweight, and highly compatible alternative that makes inserting, aligning, and locking images to cells effortless, even without complex coding.

Frequently Asked Questions
Why does my inserted image appear in the wrong cell when using VBA?
This usually happens because the macro inserts the image based on the active cell's location or default screen coordinates. To fix this, your VBA code must explicitly define the image's Top and Left properties to match the exact coordinates of your target cell.
How can I ensure the VBA code deletes only the picture in cell B1?
Instead of deleting by generic picture names, write a loop that iterates through all shapes on the ActiveSheet. Check the 'TopLeftCell.Address' property of each shape; if it equals '$B$1', then delete it. This prevents accidental deletion of images in other cells.
Does resizing an image in VBA break its aspect ratio?
It can if you force both Width and Height independently. To maintain the original proportions, make sure to set 'ShapeRange.LockAspectRatio = msoTrue' before you adjust the dimensions of the newly inserted picture.
What is the formula to perfectly center an image inside a cell using VBA?
After resizing the image to fit inside the cell, calculate the center by setting the image's Left property to 'Cell.Left + (Cell.Width - Image.Width) / 2' and its Top property to 'Cell.Top + (Cell.Height - Image.Height) / 2'.




