|
楼主 |
发表于 2025-1-11 19:08
|
显示全部楼层
本帖最后由 qdzbk 于 2025-1-11 20:14 编辑
谢谢各位老师的提示!已解决。
Sub InsertPictureExample()
Dim ws As Worksheet
Dim imgPath As String
Dim imgCell As Range
Dim pic As Shape
Set ws = ThisWorkbook.Sheets("Sheet1") ' 设置工作表
Set imgCell = ws.Range("B3") ' 设置插入图片的单元格位置
imgPath = ThisWorkbook.Path & "\图片\" & imgCell.Value ' 设置图片路径及文件名
For Each pic In ws.Shapes
If pic.TopLeftCell.Address = imgCell.Address Then
pic.Delete '删除原图片
End If
Next pic
ws.Shapes.AddPicture imgPath, msoFalse, msoCTrue, imgCell.Left, imgCell.Top, -1, -1 ' 在指定单元格的左、上位置插入图片
With ws.Shapes(ws.Shapes.Count) ' 按单元格的大小调整图片)
.LockAspectRatio = msoFalse ' 解锁纵横比
.Width = imgCell.Width '设置宽度
.Height = imgCell.Height '设置高度
End With
End Sub
|
|