The code below requires you to double click the cell where the picture is to be inserted. The code resizes the picture to the same size as the cell but this can be changed if you desire.
Firstly the code brings up the file select dialog so that you can navigate to and select the required picture. After selecting the picture, it is inserted at the cell where you double click.
Please feel free to get back to me if you have any problems either with the editing or other problems.
To install the code.
- Right click the worksheet tab and select View code to open the VBA editor.
- Copy the code below and paste into the VBA editor
- Edit the code where you see the comment in green to edit "C:C" to the required column for the pictures.
- Also edit the initial path to commence the search for the required picture file and if your file type is not included in the Filter then edit that line also.
- Close the VBA editor. (Cross at top most right corner of VBA editor)
- Use "Save as" to Save the workbook as macro enabled.
- Ensure that macros are enabled. See help for how to do this. (The option to "Disable all with notification" should be OK)
- Now you should be able to double click the cell where the picture is required and then select the file from the file dialog.
- Comments are bolded italics where editing required. (It will simply be standard green front when pasted into VBA editor.)
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim fd As FileDialog
Dim strPathAndPic As String
Dim strDialogTitle
Dim dblLeft As Double
Dim dblTop As Double
Dim dblWdth As Double
Dim dblHt As Double
Dim shpMyImg 'As Shape
'Test if double click is in the required column
'If intersect is nothing then not correct column
'Edit "C:C" to required column for pictures.
If Intersect(Target, Columns("C:C")) Is Nothing Then Exit Sub
Cancel = True 'Cancels double click that normally invokes edit in cell mode
strDialogTitle = "Select required picture file."
Set fd = Application.FileDialog _
(msoFileDialogFilePicker)
With fd
'Next line Edit path to your initial path to search for picture
.InitialFileName = "C:\Users\User\Documents\Excel\Test Macros"
.AllowMultiSelect = False
.Filters.Clear
.Filters.Add "Pictures", "*.jpg; *.bmp; *.tif", 1 'Edit and/or add file types
.Title = strDialogTitle
If .Show = False Then
MsgBox "User cancelled." & vbLf & _
"Processing terminated."
Exit Sub
End If
strPathAndPic = .SelectedItems(1)
End With
With Target
dblLeft = .Left
dblTop = .Top
dblWdth = .Width
dblHt = .Height
Set shpMyImg = Me.Shapes.AddPicture _
(Filename:=strPathAndPic, _
LinkToFile:=msoFalse, _
SaveWithDocument:=msoTrue, _
Left:=.Left, _
Top:=.Top, _
Width:=-1, _
Height:=-1)
End With
With shpMyImg
'Next line If locked then only Height or Width can be set. (Last set takes precedence)
' .LockAspectRatio = False 'Optional True or False
.Left = dblLeft
.Top = dblTop
.Width = dblWdth
.Height = dblHt
'Name the image so it can be referenced later in other subs
.Name = "Pic_" & Target.Address(0, 0) 'Names the image with Pic and cell address
.Placement = xlMoveAndSize 'Allow to move and size with the cell.
End With
End Sub