I have a situation at work where people have to manually introduce pictures in a certain page of excel and resize it also manually. As a complete very beginner I've managed to find some VBA code to help introduce the picture by clicking a button and inserting it in a certain range of cells. The problem that I have is that I cannot figure out (after searching many posts) how to correctly introduce the function to save the image without making a link to it so others can see the report without getting an error that the picture doesn't exist.
Can you kindly help me and complete where the function should be introduced?
Private Sub CommandButton3_Click()
Dim strFileName As String
Dim objPic As Picture
Dim rngDest As Range
strFileName = Application.GetOpenFilename( _
FileFilter:="Images (*.jpg;*.gif;*.png),*.jpg;*.gif;*.png", _
Title:="Please select an image...")
If strFileName = "False" Then Exit Sub
Set rngDest = Me.Range("B24:C26")
Set objPic = Me.Pictures.Insert(strFileName)
With objPic
.ShapeRange.LockAspectRatio = msoFalse
.Left = rngDest.Left
.Top = rngDest.Top
.Width = rngDest.Width
.Height = rngDest.Height
End With
End Sub
Thanks in advance!
Set objPic = Me.Pictures.Insert(strFileName)is correct. It should insert the image without making it a link. You can also use Shapes.AddPicture method (Excel). Something likeDim objPic As Objectand then in the next lineSet objPic = ActiveSheet.Shapes.AddPicture(strFileName, False, True, 10, 10, -1, -1)codeShapeRange.LockAspectRatio = msoFalseMebut if you are running this code from a userform then you should declare your worksheet object and use that. For exampleDim ws as Worksheetand thenSet ws = Sheet1and finallySet objPic = ws.Shapes.AddPicture....