VBA代码:从Excel单元格插入图片并调整大小
Sub InsertPicture() Dim i As Integer Dim filePath As String Dim cellValue As String Dim picWidth As Integer Dim picHeight As Integer
'设置图片大小
picWidth = 20
picHeight = 100
'从D1单元格开始循环判断
For i = 1 To Rows.Count
If Range("D" & i).Value = "" Then
'如果D单元格为空则跳过
Continue For
Else
'组合图片路径
cellValue = Range("D" & i).Value
filePath = "E:\资料\SZH\R2000HIEP-H照片\" & cellValue & ".jpg"
'在N单元格内插入图片
With Range("N" & i)
.Activate
.ClearContents
If Dir(filePath) <> "" Then
.Select
With .Parent.Pictures.Insert(filePath)
.Width = picWidth
.Height = picHeight
.Top = .Top + (.Height - picHeight) / 2
.Left = .Left + (.Width - picWidth) / 2
End With
Else
.Value = "图片不存在"
End If
End With
End If
Next i
End Sub
原文地址: https://www.cveoy.top/t/topic/mS4B 著作权归作者所有。请勿转载和采集!