zl程序教程

您现在的位置是:首页 >  工具

当前栏目

在excel中批量插入图片

Excel批量 图片 插入
2023-09-14 09:00:19 时间

效果图:

  

使用VBA:

Sub 批量插入图片()
Dim item As String '设置一个名为cfan字符串,将其作为图片路径变量
Dim rng As Range
Sheets("Sheet4").Select '选中要插入图片的工作表
x = [a65536].End(xlUp).Row '取得最后一行的行号
For i = 2 To x
name = Cells(i, 2) '从第2列(即B列)得到图片名称,并以此名查找指定位置的图片
cfan = "e:\Folder" & "\" & name & ".png" '指定图片实际保存位置和格式
If Dir(item) <> "" Then
Cells(i, 4).Select '图片需要插入到第4列
ActiveSheet.Pictures.Insert(item).Select
Set rng = Cells(i, 4) '根据单元格的大小调整图片
With Selection
.Top = rng.Top + 1
.Left = rng.Left + 1
.Width = rng.Width - 1
.Height = rng.Height - 1
End With
End If
Next
End Sub