使用工作簿的文件名查找具有相同文件名的图像Excel VBA

我正在testing导入一个图像到工作表,这已经certificate是成功的,我怎样才能使用我的工作簿的文件名,我存储在一个范围内,然后在预选的目录中查找具有相同文件名的图像?

我的文件名保存在Range – LkupFileName中

Sub InsertImage() Dim ws As Worksheet Dim ImgPath As String Dim W As Double, H As Double Dim L As Long, T As Long Set ws = ThisWorkbook.Sheets("myworksheet") '~~> File Location of saved JPG ImgPath = "C:\images.jpg" With ws W = 100 '<~~ Width H = 50 '<~~ Height L = .Range("H140").Left '<~~ Left Position for image T = .Range("H140").Top '<~~ Top Position for image 'Copy & Paste Image code With .Pictures.Insert(ImgPath) With .ShapeRange .LockAspectRatio = msoTrue .Width = W .Height = H End With .Left = L .Top = T .Placement = 1 End With End With End Sub 

试试这个:

 Sub InsertImage() Dim ws As Worksheet Dim ImgPath As String, ImgName As String Dim W As Double, H As Double Dim L As Long, T As Long Set ws = ThisWorkbook.Sheets("myworksheet") '~~> File Location of saved JPG ImgName = ws.Range("LkupFileName").Value ImgPath = "C:\Foo\Bar\" & ImgName & ".jpg" 'Modify accordingly. With ws W = 100 '<~~ Width H = 50 '<~~ Height L = .Range("H140").Left '<~~ Left Position for image T = .Range("H140").Top '<~~ Top Position for image 'Copy & Paste Image code With .Pictures.Insert(ImgPath) With .ShapeRange .LockAspectRatio = msoTrue .Width = W .Height = H End With .Left = L .Top = T .Placement = 1 End With End With End Sub 

假定有两件事情:

  • 通过LkupFileName ,我假设这是一个命名的范围。
  • 该图像将始终在您指定的目录中find。

让我们知道这是否有帮助。 🙂