Можете попробовать этот код и изменить его по вашему требованию.
Sub InsertPics()
Dim fPath As String, fName As String
Dim r As Range, rng As Range
Dim shpPic As Shape, IsCmnt As VbMsgBoxResult
'Application.ScreenUpdating = False
Set rng = ThisWorkbook.ActiveSheet.Range("D2:D" & Cells(Rows.Count, 4).End(xlUp).Row)
On Error GoTo Xexit
Set rng = Application.InputBox("Select the range to import Images", "Import Image", rng.Address, , , , , 8)
On Error GoTo 0
If rng Is Nothing Then Exit Sub
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = " Select Folder to Upload Images"
.AllowMultiSelect = False
.InitialFileName = "C:\Users\user\DeskTop\"
If .Show <> -1 Then Exit Sub
fPath = .SelectedItems(1)
End With
fPath = fPath & "\"
'Avoided further asking wheather all Images are to be uploaded as Comment
'instead used bold font of the file names to do the same
'try Next statement, if want all the images as comment
'IsCmnt = MsgBox("Is the images to be uploaded as comments", vbYesNo)
For Each r In rng
If r.Value <> "" Then
If Dir(fPath & r.Value & ".jpg") <> "" Then
'If IsCmnt = vbYes Then 'try this branch if want all the images as comment
If r.Font.Bold Then ' instead of asking multiple times
r.ClearComments
r.AddComment ""
r.Comment.Shape.Fill.UserPicture fPath & r.Value & ".jpg"
Else
Set shpPic = ActiveSheet.Shapes.AddPicture(Filename:=fPath & r.Value & ".jpg", linktofile:=msoFalse, _
savewithdocument:=msoTrue, Left:=Cells(r.Row, 2).Left, Top:=Cells(r.Row, 2).Top, Width:=-1, Height:=-1)
With shpPic
.LockAspectRatio = msoTrue
If .Width > Columns(2).Width Then .Width = Columns(2).Width
Rows(r.Row).RowHeight = .Height
End With
End If
Else
Debug.Print fPath & r.Value & ".jpg not found"
End If
End If
Next r
Xexit:
'Application.ScreenUpdating = True
End Sub
Код проверен с временными изображениями. Может отключить ScreenUpdating
в соответствии с фактическим состоянием.