vba to resize a picture

cjcass

Well-known Member
Joined
Oct 27, 2011
Messages
679
Office Version
  1. 2016
Platform
  1. Windows
Hi is there some vba that I can use to resize pictures in two different ways:
1. vba that reduces two pictures, named Pic 1 and Pic 2, by 10% of their existing size but the pictures need to remain at their central point, ie. the pics don't resize up to the left corner and change position.
2. vba that changes the size of the two pictures, named Pic 1 and Pic 2, by the value in cell A1, eg. the value could be 50%, or 120%, but again the pictures need to remain at their central position.
Any help much appreciated.
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Hi,​
as per kid logic to 'resize at their central point' - which does not exist in Excel as each shape references to Left & Top properties ! -​
the easy way is to compare its Height & Width properties before & after in order to add half of the difference to each one …​
 
Upvote 0
You can use the ScaleWidth and ScaleHeight methods of the Shape object. And you can set the Scale parameter to msoScaleFromMiddle so that the shape's midpoint retains it's position...




So, for example, if LockAspectRatio is set to True...

VBA Code:
    Dim shp As Shape
    Set shp = Worksheets("Sheet1").Shapes("Picture 1")
    
    shp.ScaleWidth 0.9, msoTrue, msoScaleFromMiddle

Otherwise, if LockAspectRatio is set to False...

Code:
    Dim shp As Shape
    Set shp = Worksheets("Sheet1").Shapes("Picture 1")
    
    shp.ScaleWidth 0.9, msoTrue, msoScaleFromMiddle
    shp.ScaleHeight 0.9, msoTrue, msoScaleFromMiddle

Hope this helps!
 
Upvote 0
So I was wrong as I forgot the 'scale' methods … Thanks Dom for the reminder.​
 
Upvote 0
You can use the ScaleWidth and ScaleHeight methods of the Shape object. And you can set the Scale parameter to msoScaleFromMiddle so that the shape's midpoint retains it's position...




So, for example, if LockAspectRatio is set to True...

VBA Code:
    Dim shp As Shape
    Set shp = Worksheets("Sheet1").Shapes("Picture 1")
   
    shp.ScaleWidth 0.9, msoTrue, msoScaleFromMiddle

Otherwise, if LockAspectRatio is set to False...

Code:
    Dim shp As Shape
    Set shp = Worksheets("Sheet1").Shapes("Picture 1")
   
    shp.ScaleWidth 0.9, msoTrue, msoScaleFromMiddle
    shp.ScaleHeight 0.9, msoTrue, msoScaleFromMiddle

Hope this helps!
Hi Domenic,
Sorry for the delay in replying.
That works great!!
Many thanks :)
 
Upvote 0
That's great to hear, thanks for the feedback.

Cheers!
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,251
Members
448,556
Latest member
peterhess2002

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top