Pictures Placed Using VBA - Results in Different Locations for Horizontal or Vertical

joefish

New Member
Joined
Dec 17, 2018
Messages
1
I have been using the following code for placing a picture over a cell range in Excel. The code works beautifully for pictures obtained in the horizontal format but when a picture is selected that has a vertical format, the picture is placed far far outside the intended location. It usually ends up so far to the right of the intended location you have to zoom out to 25% to find it. I have tried several variations with handling the original orientation of the picture but to no avail. Any ideas out there about what could be causing this?

Code:
Sub Insert_Picture_Page1()

Application.ScreenUpdating = False

'Insert and Size Photo
Dim getPicture As Variant
Dim myPictureCells As Range
Dim DeadPicture As Shape


'Allow user to select the photo to insert
getPicture = Application.GetOpenFilename _
        ("Pictures (*.gif; *.jpg; *.bmp; *.tif; *.png),*.gif; *.jpg; *.bmp; *.tif *.png", , "Select Picture to Import")
    If VarType(getPicture) = vbBoolean Then
        MsgBox "Operation Cancelled"
    Else


'Unlock the Cell for the Action
ActiveSheet.Unprotect


'Deletes the Previous Picture
        For Each DeadPicture In ActiveSheet.Shapes
            If DeadPicture.Name = "Page1Picture" Then
            DeadPicture.Delete
            End If
        Next DeadPicture


'Sets the insertion point
        Set myPictureCells = Range("U17:AP17")


'Name the picure so it can be deleted later when replaced (2010 Excel)
ActiveSheet.Shapes.AddPicture(getPicture, False, True, -1, -1, -1, -1).Select
Selection.Name = "Page1Picture"


'Sizes the picture to fit the box
With Selection.ShapeRange
    .LockAspectRatio = msoTrue
    .Top = Range("U17:AP17").Top
    .Left = Range("U17:AP17").Left


            If Selection.ShapeRange.Width <> 269 Then
                Selection.ShapeRange.Width = 269
            End If


            If Selection.ShapeRange.Height > 201 Then
                Selection.ShapeRange.Height = 201
            End If


'Center the image and sends behind the button
        .IncrementLeft (myPictureCells.Width - Selection.ShapeRange.Width + 1) / 2
        .IncrementTop (myPictureCells.Height - Selection.ShapeRange.Height) / 2
        .ZOrder msoSendToBack
    
    End With


End If
    
'Protects the Worksheet Again
        myPictureCells.Locked = True
        ActiveSheet.Protect DrawingObjects:=False, Contents:=True
        
End Sub
 

Forum statistics

Threads
1,081,661
Messages
5,360,338
Members
400,581
Latest member
Eskimo

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top