In this post we will see how to fill a shape with image in excel.
I am going to explain how we could automatically fit an image inside a shape in excel, we could insert an image inside a shape manually also but here in this post we will how we can do that with the help of excel macro.
If you have tried to fill an image inside shape then you would know how annoying this task is as the image needs to be adjusted so carefully within the shape and sometimes the result is a distorted image somehow fit inside the shape.
With the help of vba we could get rid of adjusting the image manually inside a shape.
Below is an example in which we can see we have a rectangular shape in excel sheet and we want to fit an image in it.
The name of the rectangle is “”Rectangle 1”.
We will create a macro in excel to auto insert an image inside a shape.
Open VB editor from Developer tab in excel as shown in the figure.
Insert a module in which we will write a small macro to fit an image inside a shape.
Now double click the newly created module to open that and paste the below code in the module.
Public Sub AddPicAndAdjust()
Dim shp As ShapeRange
Set shp = ActiveSheet.Shapes.Range(Array(“Rectangle 1”))
.Visible = msoTrue
.UserPicture “C:\Users\rvdsr\Desktop\Flower.png” ‘<== Add pic location
.TextureTile = msoFalse
.RotateWithObject = msoTrue
.LockAspectRatio = msoFalse
This program defines an object for shape and assigns the picture given in the location to this object.
Run the program by pressing F5 or by play button.
You can see that after the program is run, we have the image inserted within the shape.
Edit the location to specify the path for your image.
Hope this helped.