To import, crop, rotate, delete, position the image in VBA Excel.
Here we have imported an image in the excel sheet then imported,cropped , rotated ,position it.We can also delete the image, if required.
What this code does
- Import a jpeg file in the excel file
- Crop, rotate , delete the file
- Change the position of the file
We have taken the below shown steps in the code-
- Import the image from local drive path to excel sheet
- Crop the image from left,right,top,bottom
- Rotate the image to 10 degree
- Position the image from left and top
- Delete part is commented in code
Image shown just below is the original image on a excel worksheet.
Now in this second image shown below, you can see that black colour borders have been cropped and image is rotated to right.
Sub InsertPicture() Dim MyPicture As Shape 'Add a image in the sheet1 Set MyPicture = Sheets("sheet1").Shapes.AddPicture("C:\Users\yourusername\Desktop\test1\Crop_Image\sampleimage.jpg", True, True, 100, 100, 500, 500) 'Crop the imported image With MyPicture.PictureFormat .CropLeft = 40 .CropRight = 60 .CropTop = 40 .CropBottom = 40 End With 'Rotate and change the position of image With MyPicture .IncrementRotation 10 .Left = 200 .Top = 200 End With 'Delete the impage,if needed 'MyPicture.Delete End Sub
- You can use this approach when you want to import an image or crop a logo.
- There are other methods which you can use to flip, count etc.
Post you may like