macros to insert picture in active cell, but picture appears more shifted out of position the higher the row

needhelp9009

New Member
Joined
Mar 30, 2021
Messages
10
Office Version
  1. 365
Platform
  1. Windows
I am using a macros to insert pictures into the active cell.
i.e. click B3, run macros and choose picture from the file. this works well in row 3. However, as I continue adding pictures going down the rows. example B90, the picture appears shifted out of the box..
Capture6.JPG
Capture7.JPG


This is my code:
VBA Code:
Sub test()
Dim myPic As String, Pic As Shape, Rng As Range
Dim a As Double, shp As Shape, x As Long, j As Long, jj As Long


    For Each shp In ActiveSheet.Shapes
        If Left(shp.Name, 7) = "Picture" Then x = x + 1
    Next shp
   
myPic = Application.GetOpenFilename( _
    FileFilter:="Images (*.jpg;*.gif;*.png),*.jpg;*.gif;*.png", _
        Title:="Please select an image...")
    If myPic = "False" Then Exit Sub

Set Pic = Application.ActiveSheet.Shapes.AddPicture(myPic, False, True, 0, 0, -1, -1)

j = ActiveCell.Row
jj = ActiveCell.Column


With Pic
    .Name = "Picture" & x + 1
    .LockAspectRatio = False
    .Left = Cells(j, jj).Left
    .Top = Cells(j, jj).Top - (j * 1 / 100)
    .Height = ActiveCell.Height
    .Width = ActiveCell.Width
   
End With

End Sub
 
Last edited by a moderator:

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
When you right click on the picture in your sheet that is not properly lined up, you don't get the same as the top picture?
If you do and click on "Size and Properties" and at the pop up at top select "Size", do you see "Rotation"?
If you do, what does it show there?
 

Attachments

  • Size and Properties.JPG
    Size and Properties.JPG
    115.7 KB · Views: 9
Upvote 0

Forum statistics

Threads
1,223,969
Messages
6,175,682
Members
452,667
Latest member
vanessavalentino83

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