VBA – Excel – 如何检索匹配文件名上的照片并将其放置在单元格中

我有一个Excel表,其中列A有一个产品代码列表。 我也有一个文件夹,每个产品的图片和图片的文件名是产品代码。 我想把每个产品的图片放在B栏旁边的代码旁边。 如果可能的话,我也想重新格式化图片,使其适合在单元格中。

我真的不知道从哪里开始,任何帮助将不胜感激! 谢谢

这是一个代码开始。 请在你身边testing一下。

  Sub AddPictures() Dim myPic As Picture Dim wkSheet As Worksheet Dim myRng As Range Dim myCell As Range Dim rowCount As Long Dim rowCount2 As Long Set wkSheet = Sheets(2) ' -- Change to your sheet '-- The usual way of finding used row count for specific column rowCount2 = wkSheet.Cells(wkSheet.Rows.Count, "C").End(xlUp).Row If rowCount2 <> 0 Then Set myRng = wkSheet.Range("C2", wkSheet.Cells(wkSheet.Rows.Count, "C").End(xlUp)) For Each myCell In myRng.Cells If Trim(myCell.Value) = "" Then MsgBox "No file path" ElseIf Dir(CStr(myCell.Value)) = "" Then MsgBox myCell.Value & " Doesn't exist!" Else myCell.Offset(0, 1).Parent.Pictures.Insert (myCell.Value) Set myPic = myCell.Parent.Pictures.Insert(myCell.Value) With myCell.Offset(0, 1) '1 columns to the right of C ( is D) '-- resize image here to fit into the size of your cell myPic.Top = .Top myPic.Width = .Width myPic.Height = .Height myPic.Left = .Left myPic.Placement = xlMoveAndSize End With End If Next myCell Else MsgBox "There is no file paths in your column" End If End Sub 

输出:

在这里输入图像说明


PS:根据你的值设置范围,文件path。 如果你需要search整个工作表中使用的REAL列和行,让我知道。 我可以分享一个function。 现在你只需要在特定的文件path列中find使用的行,所以上面的典型使用的行数就足够了。