I have a problem integrating my InsertPicture code to my FitPicture macro. I'm confused on how to get the shape to resize it automatically after using Insert function. It gives me the error regarding with the object. Here's a link of the idea that I research but still can't make anything happen. Any help is appreciated. Thanks.
Here's the macro I'm using to fit the picture into a merged cell or single cell:
Sub FitPicture()
On Error GoTo NOT_SHAPE
Dim r As Range, sel As Shape
Set sel = ActiveSheet.Shapes(Selection.Name)
sel.LockAspectRatio = msoTrue
Set r = Range(sel.TopLeftCell.MergeArea.Address)
Select Case (r.Width / r.Height) / (sel.Width / sel.Height)
Case Is > 1
sel.Height = r.Height * 0.9
Case Else
sel.Width = r.Width * 0.9
End Select
sel.Top = r.Top + 0.05 * sel.Height: sel.Left = r.Left + 0.05 * sel.Width
Exit Sub
NOT_SHAPE:
MsgBox "Please select a picture first."
End Sub
Here's the macro I'm using to insert a picture:
Sub InsertPicture()
Dim sPicture As String, pic As Picture
sPicture = Application.GetOpenFilename _
("Pictures (*.gif; *.jpg; *.bmp; *.tif), *.gif; *.jpg; *.bmp; *.tif", _
, "Select Picture to Import")
If sPicture = "False" Then Exit Sub
Set pic = ActiveSheet.Pictures.Insert(sPicture)
With pic
.ShapeRange.LockAspectRatio = msoFalse
.Height = ActiveCell.Height
.Width = ActiveCell.Width
.Top = ActiveCell.Top
.Left = ActiveCell.Left
.Placement = xlMoveAndSize
End With
Set pic = Nothing
End Sub
How can I integrate my FitPicture code to InsertPicture code? I need to resize it automatically after inserting using my mentioned modification on FitPicture. By the way, I'm using excel 2013. Thanks mates.