VBA: Move and Size Pics

NeoSez

Board Regular
Joined
Aug 14, 2020
Messages
210
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
Hello. I am trying resize all the photos in all the sheets to be 3 inches in width only, but maintain aspect ratio. I do not want the photos to move and size with cells. Photos should move with the cells but don't size with cells. It should always maintain the exact width of 3 inches.

Here is what I have so far and it's not working. Please help tweak. Thank you.

VBA Code:
Sub MoveButDontSizeWithCells()
    Dim xPic As Picture
    On Error Resume Next
    Application.ScreenUpdating = False
    For Each xPic In ActiveSheet.Pictures
        xPic.Placement = xlMoveButDontSize
        .ShapeRange.LockAspectRatio =msoTrue
        .Width = 300
    Next
    Application.ScreenUpdating = True
End Sub
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
When I commented out On Error Resume Next, I got the following error...

VBA Code:
Compile error:

Invalid or unqualified reference

...and it highlited this lines...

VBA Code:
.ShapeRange.LockAspectRatio =msoTrue

So you can easily see that ShapeRange needs to be qualified with a reference to the Picture object...

VBA Code:
xPic.ShapeRange.LockAspectRatio =msoTrue

And, the same thing happened with .Width. Also, the Placement property should be assigned xlMove. So you macro should be amended as follows...

VBA Code:
Sub MoveButDontSizeWithCells()
    Dim xPic As Picture
    'On Error Resume Next
    Application.ScreenUpdating = False
    For Each xPic In ActiveSheet.Pictures
        With xPic
            .Placement = xlMove
            .ShapeRange.LockAspectRatio = msoTrue
            .Width = 300
        End With
    Next
    Application.ScreenUpdating = True
End Sub

However, I would suggest looping through the Shapes collection instead of Pictures, and then test whether the shape is in fact a picture. Otherwise you'll get an error if your worksheet contains other objects, such as an ActiveX commandbutton.

VBA Code:
Sub MoveButDontSizeWithCells()
    Dim shp As Shape
    Application.ScreenUpdating = False
    For Each shp In ActiveSheet.Shapes
        If shp.Type = msoPicture Then
            With shp
                .Placement = xlMove
                .LockAspectRatio = msoTrue
                .Width = 300
            End With
        End If
    Next
    Application.ScreenUpdating = True
End Sub

Hope this helps!
 
Upvote 0
When I commented out On Error Resume Next, I got the following error...

VBA Code:
Compile error:

Invalid or unqualified reference

...and it highlited this lines...

VBA Code:
.ShapeRange.LockAspectRatio =msoTrue

So you can easily see that ShapeRange needs to be qualified with a reference to the Picture object...

VBA Code:
xPic.ShapeRange.LockAspectRatio =msoTrue

And, the same thing happened with .Width. Also, the Placement property should be assigned xlMove. So you macro should be amended as follows...

VBA Code:
Sub MoveButDontSizeWithCells()
    Dim xPic As Picture
    'On Error Resume Next
    Application.ScreenUpdating = False
    For Each xPic In ActiveSheet.Pictures
        With xPic
            .Placement = xlMove
            .ShapeRange.LockAspectRatio = msoTrue
            .Width = 300
        End With
    Next
    Application.ScreenUpdating = True
End Sub

However, I would suggest looping through the Shapes collection instead of Pictures, and then test whether the shape is in fact a picture. Otherwise you'll get an error if your worksheet contains other objects, such as an ActiveX commandbutton.

VBA Code:
Sub MoveButDontSizeWithCells()
    Dim shp As Shape
    Application.ScreenUpdating = False
    For Each shp In ActiveSheet.Shapes
        If shp.Type = msoPicture Then
            With shp
                .Placement = xlMove
                .LockAspectRatio = msoTrue
                .Width = 300
            End With
        End If
    Next
    Application.ScreenUpdating = True
End Sub

Hope this helps!

Domenic

Thank you so much for the explanations. As a noob, it really helps me learn.
Just tried your script and it works really well. However, I have a couple of questions.
  1. How do I get it to run for all the sheets in the Workbook?
  2. How to get the resized photos to stay inside the cell?
 
Upvote 0
To run the macro for all worksheets within the active workbook, try...

VBA Code:
Sub MoveButDontSizeWithCells()
    Dim ws As Worksheet
    Dim shp As Shape
    Application.ScreenUpdating = False
    For Each ws In ActiveWorkbook.Worksheets
        For Each shp In ws.Shapes
            If shp.Type = msoPicture Then
                With shp
                    .Placement = xlMove
                    .LockAspectRatio = msoTrue
                    .Width = 300
                End With
            End If
        Next shp
    Next ws
    Application.ScreenUpdating = True
End Sub

For your second question, I'm unclear. Can you please elaborate?
 
Upvote 0
Solution
To run the macro for all worksheets within the active workbook, try...

VBA Code:
Sub MoveButDontSizeWithCells()
    Dim ws As Worksheet
    Dim shp As Shape
    Application.ScreenUpdating = False
    For Each ws In ActiveWorkbook.Worksheets
        For Each shp In ws.Shapes
            If shp.Type = msoPicture Then
                With shp
                    .Placement = xlMove
                    .LockAspectRatio = msoTrue
                    .Width = 300
                End With
            End If
        Next shp
    Next ws
    Application.ScreenUpdating = True
End Sub

For your second question, I'm unclear. Can you please elaborate?
For your second question, I'm unclear. Can you please elaborate?
Thank you for the quick response.
The second part, there are some pics that are long and it extends into the cells below. How to make it fit into the cell without extending into surrounding cells.
 
Upvote 0

Forum statistics

Threads
1,214,615
Messages
6,120,538
Members
448,970
Latest member
kennimack

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