Sub InsertPicture()
' Select the destination cell
Range("A11").Select
' Assuming that value "Daisy.jpg" is in cell H1 and the picture is in folder "C:\TEMP\"
' the below code inserts picture C:\TEMP\Daisy.jpg to the left top corner of the selected cell A11
ActiveSheet.Pictures.Insert "C:\TEMP\" & Range("H1").Value
End Sub
Sub InsertPicture_TestVersion()
' --> Settings, change to suit
Const Folder = "C:\TEMP\" ' Folder with picture
Const CellWithFilename = "H1" ' Cell with filename
Const DestinationCell = "A11" ' Cell to insert the picture
' <-- End of settings
Dim f As String, PathSep As String
' Apply correct path separator
PathSep = Application.PathSeparator
f = Replace(Folder, "\", PathSep)
If Right(f, 1) <> PathSep Then f = f & PathSep
' Check the folder
If Dir(f, vbDirectory) = "" Then
MsgBox "Folder not found:" & vbLf & f, vbCritical, "Error1"
Exit Sub
End If
' Check file extension
If InStr(Range(CellWithFilename).Value, ".") = 0 Then
Range(CellWithFilename).Select
MsgBox "File extension not found in " & CellWithFilename, vbCritical, "Error2"
Exit Sub
End If
' Check the file
f = f & Trim(Range(CellWithFilename).Value)
If Dir(f) = "" Then
MsgBox "File not found:" & vbLf & f, vbCritical, "Error3"
Exit Sub
End If
' Check sheet protection
If ActiveSheet.ProtectContents Then
MsgBox "Unprotect the sheet, please", vbCritical, "Error4"
Exit Sub
End If
' Select the destination cell
Range(DestinationCell).Select
' Insert the picture
On Error Resume Next
ActiveSheet.Pictures.Insert f
If Err Then
MsgBox Err.Description, vbCritical, "Error#" & Err.Number
End If
End Sub
Sub InsertPicture3()
' --> Settings, change to suit
Const Folder = "C:\TEMP\" ' Folder with picture
Const CellWithFilename = "H1" ' Cell with filename
Const DestinationCell = "A11" ' Cell to insert the picture
' <-- end of settings
Dim f As String, PathSep As String
' Apply correct path separator
PathSep = Application.PathSeparator
f = Replace(Folder, "\", PathSep)
' Add backslash if it was missed
If Right(f, 1) <> PathSep Then f = f & PathSep
' Check the folder
If Dir(f, vbDirectory) = "" Then
MsgBox "Folder not found:" & vbLf & f, vbCritical, "Error1"
Exit Sub
End If
' Check file extension
If InStr(Range(CellWithFilename).Value, ".") = 0 Then
Range(CellWithFilename).Select
MsgBox "File extension not found in " & CellWithFilename, vbCritical, "Error2"
Exit Sub
End If
' Check the file
f = f & Trim(Range(CellWithFilename).Value)
If Dir(f) = "" Then
MsgBox "File not found:" & vbLf & f, vbCritical, "Error3"
Exit Sub
End If
' Check sheet protection
If ActiveSheet.ProtectContents Then
MsgBox "Unprotect the sheet, please", vbCritical, "Error4"
Exit Sub
End If
' Select the destination cell
Range(DestinationCell).Select
' Insert the picture and resize it
On Error Resume Next
With ActiveSheet.Pictures.Insert(f)
.ShapeRange.LockAspectRatio = msoFalse
.Placement = xlMoveAndSize
.Width = Range(DestinationCell).Width
.Height = Range(DestinationCell).Height
End With
If Err Then
MsgBox Err.Description, vbCritical, "Error#" & Err.Number
End If
End Sub