需要代码来扫描文件夹Z:\Images
并在excel单元格中插入匹配的图像:
以下代码用于插入连续的单元格,而不是随机单元格。我不知道如何根据自己的要求进行修改。
Option Explicit
Sub Insert()
Dim strFolder As String
Dim strFileName As String
Dim objPic As Picture
Dim rngCell As Range
strFolder = "C:\Users\Domenic\Pictures\Saved Pictures\" 'change the path accordingly
If Right(strFolder, 1) <> "\" Then
strFolder = strFolder & "\"
End If
Set rngCell = Range("E1") 'starting cell
strFileName = Dir(strFolder & "*.png", vbNormal) 'filter for .png files
Do While Len(strFileName) > 0
Set objPic = ActiveSheet.Pictures.Insert(strFolder & strFileName)
With objPic
.Left = rngCell.Left
.Top = rngCell.Top
.Height = rngCell.RowHeight
.Placement = xlMoveAndSize
End With
Set rngCell = rngCell.Offset(1, 0)
strFileName = Dir
Loop
End Sub
该代码能够获取我需要的数据(图像文件),但是它将图像粘贴到一列中-范围(“ E1”)。
我需要编写一个if条件以匹配excel中的图像名称,并在找到匹配项时粘贴图像。