我有一个具有160行和2列数据的Excel文件-文章名称,价格。我也有一个文件夹,其中包含这些文章的照片。
问题在于图片名称与我的Excel工作表中的文章名称不完全相同。
例如,在我的表我有商品名:“3714-012-P140”,但该文件夹中这将是“3714-012-P140- - - ****”。
但是,在最初的3个代码块(示例中为3714; 012; P140)之后,在搜索中始终只显示1张图片。
如何选择带有通配符的图片?
此外,我将如何将图片锁定到excel中的特定单元格中?我的意思是说,当我调整大小或删除某些行/列时,图片会沿着分配给它们的单元格移动。
Dim ws As Worksheet
Dim articleCode As String, _
findStr As String
Set ws = Workbooks(1).Worksheets(1)
For i = 1 to ws.UsedRange.Rows.Count
articleCode = ws.Cells(i,1)
findStr = 'some code
ActiveSheet.Pictures.Insert( _
"C:\...path...\" & findStr & ".jpg").Select
Next i
编辑:我需要将照片插入到数据的每一行的第三列中。
下面的函数采用articleCode,即图片的名称,应在其中输入图片的行和列。
Function picInsert(articleCode As String, row As Integer, column As Integer)
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim i As Integer
Dim ws As Worksheet
Set ws = Workbooks(1).Worksheets(2) 'your worksheet where the pictures will be put
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("...path...")
i = 1
For Each objFile In objFolder.Files
If objFile.name Like (articleCode & "*") Then 'finds a picture with similar name to the one searched
With ActiveSheet.Pictures.Insert(objFile.Path)
With .ShapeRange
.LockAspectRatio = msoTrue
.Width = 5
.Height = 15
End With
.Left = ActiveSheet.Cells(row, column).Left
.Top = ActiveSheet.Cells(row, column).Top
.Placement = 1 'locks the picture to a cell
End With
End If
i = i + 1
Next objFile
End Function
这是我尝试上述功能的测试子。基本上,一个简单的循环遍历行,使用上面的函数从第一列获取articleCode并将图片输入第三列。
Public Sub test()
Dim ws As Worksheet
Dim i As Integer
Dim articleCode As String
Set ws = Workbooks(1).Worksheets(2)
For i = 1 To ws.UsedRange.Rows.Count
articleCode = ws.Cells(i, 1)
Call picInsert(articleCode, i, 3)
Next i
End Sub
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句