how to resize image to desired size after selecting it using vba
- User
how to resize image to desired size after selecting it using vba
- Assistant
To resize an image to a desired size after selecting it using VBA, you can use the following steps:
- First, select the image by using the
Selectmethod. You can use theShapescollection to reference the selected image shape.
vbaDim img As Shape Set img = ActiveSheet.Shapes("Image1") ' Change "Image1" to the name or index of your image img.Select- Next, specify the desired dimensions (width and height) for the resized image. You can use the
WidthandHeightproperties of the image shape to update its size.
- First, select the image by using the
- User
I want to make a vba code. I will paste an image after selecting the image i will run an vba code that will resize the image to Height = 1.64" and width = 2.21"
- Assistant
Sure! Here's the VBA code to achieve that:
vbaSub ResizeImage() Dim img As Shape Dim desiredWidth As Double Dim desiredHeight As Double ' Select the pasted image Set img = ActiveSheet.Shapes(Selection.Name) ' Set desired width and height desiredWidth = 2.21 desiredHeight = 1.64 ' Resize the image img.Select With img .LockAspectRatio = msoFalse .Width = Application.InchesToPoints(desiredWidth) .Height = Application.InchesToPoints(desiredHeight) End With End SubHere's how to use this code:
- Paste an image onto your worksheet by copying and pasting it or inserting it from a file.
- Select the image you want to resize.
- Run the macro (either by pressing to open the "Macro" dialog or assigning the macro to a button on your worksheet).