可以用vba代码完成:
1、先编制表格:
2、在表格里编写触发宏代码:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim photoname As String
If Target.Row = 3 And Target.Column > 3 And Target.Column < 6 Then
On Error Resume Next '忽略错误继续执行VBA代码,避免出现错误消息
Application.ScreenUpdating = False
Application.EnableEvents = False
For Each shp In Sheets("查询表").Shapes
If shp.Type <> 8 And shp.Type <> 12 Then
shp.Delete
End If
Next
photoname = Cells(3, 4) & ".JPG"
Cells(3, "L").Select
ActiveSheet.Pictures.Insert(ActiveWorkbook.Path & "\照片\" & photoname).Select '当前文件所在目录下以单元内容为名称的.jpg图片
With Selection
ta = Range(Cells(3, "L").MergeArea.Address).Height '单元高度
tb = Range(Cells(3, "L").MergeArea.Address).Width '单元宽度
tc = .Height '图片高度
td = .Width '图片宽度
tm = Application.WorksheetFunction.Min(ta / tc, tb / td) '单元与图片之间长宽差异比例的最小值
.Top = ActiveCell.Top + 2
.Left = ActiveCell.Left + 1
.Height = .Height * tm * 0.98 '按比例调整图片宽度
.Width = .Width * tm * 0.98 '按比例调整图片高度
End With
Cells(3, 4).Select
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
3、在当前目录下建个名为”照片“的子目录,里面存有以姓名为名称的.jpg格式的照片
4、在姓名后单元输入姓名后,就能自动插入图片了
sub kkk()
aa = ActiveWorkbook.Sheets.Count
For i = 1 To aa
Worksheets(i).Select
Worksheets(i).Range("A2").Select
Sheets(i).Range("A2").RowHeight = 60
ActiveSheet.Shapes.AddShape(msoShapeRectangle, 0, 48, 72, 60).Select
Selection.ShapeRange.Fill.Visible = msoFalse
Selection.ShapeRange.Shadow.Obscured = msoTrue
Selection.ShapeRange.Shadow.Type = msoShadow18
Selection.ShapeRange.Fill.UserPicture "d:\pic\" & Worksheets(i).Range("A1").Value & ".jpg"
Next i
end sub
每个工作表的A1单元格应输入“王二”等姓名,而且与D:\pic 目录下的图片文件对应