VBA – Insert an image/picture in Excel and position it correctly

Inserting an image to Excel with VBA is actually one line:

ThisWorkbook.Worksheets(1).Pictures.Insert ("C:\Users\myPic.png")

The interesting part happens, when it should be correctly positioned. In order to position it the way we want, we need to give 3 parameters – the image top position, the image left position and the width (or the height) of the image. If the width is given, then the height is calculated, thus keeping the image scale:

The KillAllShapes part of the code deletes all shapes from the worksheet, given as parameter. Then, using .Top , .Left and .Width the image is positioned correctly, as per the location of the mentioned Ranges. The myShape.IncrementRotation 360 rotates the image into 360 degrees, thus keeping it as it was. Anyway, it could be changed to 90, 180 or 270, depending on the requirements:

That’s all, folks!

Tagged with: , , , , ,