我根据参考资料试过了。
Sub AddPictures()
Dim myPic As Picture
Dim wkSheet As Worksheet
Dim myRng As Range
Dim myCell As Range
Dim rowCount2 As Long
Set wkSheet = Sheets(2) ' -- Working sheet
'-- The usual way of finding used row count for specific column
rowCount2 = wkSheet.Cells(wkSheet.Rows.Count, "A").End(xlUp).Row
If rowCount2 <> 0 Then
Set myRng = wkSheet.Range("A2", wkSheet.Cells(wkSheet.Rows.Count, "A").End(xlUp)) 'S.no starting from cell A2
For Each myCell In myRng.Cells
If Len(myCell) = 1 Then
myCell2 = "E_00" & myCell & ".jpg"
myCell3 = "S_00" & myCell & ".jpg"
ElseIf Len(myCell) = 2 Then
myCell2 = "E_0" & myCell & ".jpg"
myCell3 = "S_0" & myCell & ".jpg"
Else
myCell2 = "E_" & myCell & ".jpg"
myCell3 = "S_" & myCell & ".jpg"
End If
myCell1 = "c:\iamges\\\" & myCell2
If Trim(myCell1) = "" Then
MsgBox "No file path"
ElseIf Dir(CStr(myCell1)) = "" Then
MsgBox "Error Image" & myCell & " Doesn't exist!"
Else
Set myPic = myCell.Offset(0, 1).Parent.Pictures.Insert(myCell1)
With myPic '1 columns to the right of A ( is B)
'-- resize image here to fit into the size of your cell
.ShapeRange.LockAspectRatio = False
myPic.Top = myCell.Offset(0, 1).Top
myPic.Width = myCell.Offset(0, 1).Width
myPic.Height = myCell.Offset(0, 1).Height
myPic.Left = myCell.Offset(0, 1).Left
myPic.Placement = xlMoveAndSize
End With
End If
myCell1 = "c:\iamges\\\" & myCell3
If Trim(myCell1) = "" Then
MsgBox "No file path"
ElseIf Dir(CStr(myCell1)) = "" Then
MsgBox "Solution image " & myCell & " Doesn't exist!"
Else
'myCell.Offset(0, 1).Parent.Pictures.Insert (myCell1)
Set myPic = myCell.Offset(0, 2).Parent.Pictures.Insert(myCell1)
With myPic '1 columns to the right of A ( is C)
'-- resize image here to fit into the size of your cell
.ShapeRange.LockAspectRatio = False
myPic.Top = myCell.Offset(0, 2).Top
myPic.Width = myCell.Offset(0, 2).Width
myPic.Height = myCell.Offset(0, 2).Height
myPic.Left = myCell.Offset(0, 2).Left
myPic.Placement = xlMoveAndSize
End With
End If
Next myCell
Else
MsgBox "File is Empty"
End If
End Sub
来自
reading the image