Resize any selected object

P

Pat

ActiveSheet.Shapes("Picture 1").Select
Selection.ShapeRange.LockAspectRatio = msoFalse
Selection.ShapeRange.Height = 70.5
Selection.ShapeRange.Width = 70.5
Selection.ShapeRange.Rotation = 0#

The above will resize Picture 1. I want to resize any selected picture.
Much appreciate any help.

Pat
 
D

Dave Peterson

Do you want to resize all the pictures on that worksheet?

If yes:

Option Explicit
Sub testme()

Dim wks As Worksheet
Dim myPict As Picture

Set wks = ActiveSheet

With wks
For Each myPict In .Pictures
With myPict.ShapeRange
.LockAspectRatio = msoFalse
.Height = 70.5
.Width = 70.5
.Rotation = 0
End With
Next myPict
End With

End Sub

If only the selected picture:

Option Explicit
Sub testme()
If TypeName(Selection) = "Picture" Then
Selection.ShapeRange.LockAspectRatio = msoFalse
Selection.ShapeRange.Height = 70.5
Selection.ShapeRange.Width = 70.5
Selection.ShapeRange.Rotation = 0#
Else
MsgBox "Please select a picture"
End If
End Sub
 
P

Pat

Yes only the selected picture.

I am getting the message "Please select a picture" even though a picture is
selected.

I placed the code into module1, then the code is executed from a
commandbutton in sheet2. I have become a little bit rusty in using VBA.

Pat
 
D

Dave Peterson

If the code is executed by clicking on a commandbutton in sheet2, then the code
should be in that sheet2 module--even if your _click event calls the routine in
the General module.

Anyway, I'm guessing that the picture and the commandbutton are both on Sheet1.

This is the code that I used for the commandbutton:

Option Explicit
Private Sub CommandButton1_Click()
If TypeName(Selection) = "Picture" Then
Selection.ShapeRange.LockAspectRatio = msoFalse
Selection.ShapeRange.Height = 70.5
Selection.ShapeRange.Width = 70.5
Selection.ShapeRange.Rotation = 0#
Else
MsgBox "Please select a picture"
End If
End Sub

But the important thing to change is how that commandbutton is treated.

Go into design mode (another icon on the control toolbox toolbar).
right click on your commandbutton
choose properties
change .takefocusonclick to false
And close the properties window
and get out of design mode.

When you clicked on the commandbutton, that became the object selected. And
then the code wouldn't work the way you wanted.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top