I am trying to insert a picture in certain positions relative to my active cell. I am working with the code below however I am having issues:
1. Sometimes the picture will insert higher than expected. I believe this has something to do with the height of the row but I am not sure.
2. I am new to VBA so most of my work is trial and error. I have had success adjusting the position by changing the offset on CenterH and CenterV
How should I go about changing the position? Is there a better way insert pictures?
Thanks!
1. Sometimes the picture will insert higher than expected. I believe this has something to do with the height of the row but I am not sure.
2. I am new to VBA so most of my work is trial and error. I have had success adjusting the position by changing the offset on CenterH and CenterV
How should I go about changing the position? Is there a better way insert pictures?
Thanks!
Code:
Sub FrstRev_PYCHECK()
InsertPicture "picture_path", _
ActiveCell, True, True 'changed range to ActiveCell
End Sub
Sub InsertPicture(PictureFileName As String, TargetCell As Range, _
CenterH As Boolean, CenterV As Boolean)
' inserts a picture at the top left position of TargetCell
' the picture can be centered horizontally and/or vertically
Dim p As Object, t As Double, l As Double, w As Double, h As Double
If Dir(PictureFileName) = "" Then Exit Sub
' import picture
Set p = ActiveSheet.Pictures.Insert(PictureFileName)
' determine positions
With TargetCell
t = .Top
l = .Left
If CenterH Then
w = .Offset(0, -0.5).Left - .Left
l = l + w / 2 - p.Width / 2
End If
If CenterV Then
h = .Offset(-1, 0).Top - .Top
t = t + h / 2 - p.Height / 2
End If
End With
' position picture
With p
.Top = t
.Left = l
End With
Set p = Nothing
End Sub
Last edited: