Replace picture on multiple sheets, maximize height or width with VBA Code

Go1rish

New Member
Joined
Mar 25, 2014
Messages
5
Hello. I have built a code that will replace an image in 3 separate sheets in my workbook; however- sometimes the images are "portrait", sometimes, they are "landscape." I want to max out the size of the image when the pictures are replaced. (By the way - each sheet has the image placed at a separate size.)
Working Form -- max width: 125, maximum height: 160 (Largest size possible is 125 x 160)
Set-up Sheet --- max width: 253, maximum height: 310 (Largest size possible is 253 x 310)
Quote Sheet --- max width: 360, maximum height: 465 (Largest size possible is 360 x 465)

The code (as below) works brilliantly when the image formats are always in he aspect ratio of 8.5" x 11" and in "portrait" orientation.
How man I modify this code so no matter what the orientation or size, the image will always be maxed out without going beyond these parameters?

VBA Code:
Sub SwapPic()

 
    Dim PicFileName As String
    With Application.FileDialog(msoFileDialogFilePicker)
        .Show
        On Error Resume Next
            PicFileName = .SelectedItems(1)
        On Error GoTo 0
    End With
    If PicFileName = "" Then Exit Sub

   With ActiveSheet.Shapes(Application.Caller)
        .TopLeftCell.Select
        .Delete
    End With
      
    With ActiveSheet.Pictures.Insert(PicFileName)
        .Name = "UserPic"
        .OnAction = "SwapPic"
    End With
   
    With ActiveSheet.Shapes.Range(Array("UserPic")).Select
    Selection.ShapeRange.Width = 125
    End With
   
'Delete Pic on Set-up Sheet
With Sheets("Set-up Sheet").Select
    ActiveSheet.Shapes.Range(Array("UserPic")).Select
    Selection.Delete
End With

'Delete Pic on Quote Sheet
With Sheets("Quote Sheet").Select
    ActiveSheet.Shapes.Range(Array("UserPic")).Select
    Selection.Delete
End With

'Copy Working Form Pic
With Sheets("Working Form").Select
    ActiveSheet.Shapes.Range(Array("UserPic")).Select
    Selection.Copy
End With

'Paste to Set-up Sheet and Adjust Size
With Sheets("Set-up Sheet").Select
    Range("A183:A203").Select
    ActiveSheet.Paste
    ActiveSheet.Shapes.Range(Array("UserPic")).Select
    Selection.ShapeRange.Width = 240
End With

'Paste to Quote Sheet and adjust size
With Sheets("Quote Sheet").Select
Range("B8:D25").Select
ActiveSheet.Paste
    ActiveSheet.Shapes.Range(Array("UserPic")).Select
    Selection.ShapeRange.Width = 360
End With

'back to Working Form
With Sheets("Working Form").Select
     Range("I3:N3").Select
End With
   
End Sub

P.S. - Thank you to whoever shared "Swap Pic." ❤️ I love it.
 
Last edited by a moderator:

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
I figured out my own answer - I used an "If-else" function. On my images I added:
With ActiveSheet.Shapes.Range(Array("UserPic")).Select
If Selection.ShapeRange.Height < Selection.ShapeRange.Width Then
Selection.ShapeRange.Height = 125
Else: Selection.ShapeRange.Width = 125
End If
End With

I will do this for the rest of the images, too.
 
Upvote 0
Solution

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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