我开发了代码来从一个文件夹(由用户选择)中搜索图像,并通过一些循环将其放在Excel的一列中。
现在我尝试使用多维数组,这样它也可以将图像插入到多个列中。我尝试创建一个循环,这样它也可以将图像插入到B列中,而不是将图像放在A列中。
我目前取得的成就:
Sub ReadFolder()
'
'ReadFolder
'
Dim File As Variant
Dim Counter As Long
Dim DirectoryList() As String
Dim varResp As Variant
Dim shape as Excel.shape
ReDim DirectoryList(1000)
' check if the user inserted a valid path or if he canceled the operation and offer him a chance to abort the operation or retry
lblTryAgain:
varResp = InputBox("Type down the files path)", "Path")
If Trim(varResp) = "" Then
If MsgBox("Do you wish to abort?", vbYesNo + vbQuestion, "Abort?") = vbYes Then
GoTo lblExit
Else
GoTo lblTryAgain
End If
Else
File = Dir$(varResp & "\*.*")
If File = "" Then
MsgBox "The path doesnt exist , Please retry", vbExclamation, "Fail"
GoTo lblTryAgain
End If
End If
On Error GoTo Erro
' fill the array with elements that are inside the file(gotta put then into a 2d array with the dimension (n,2)
Do While File <> ""
DirectoryList(Counter) = File
File = Dir$
Counter = Counter + 1
Loop
' resize the array accordingly to the number of elements filled inside it
ReDim Preserve DirectoryList(Counter - 1)
' delete the images inside the sheet before inserting new ones
For Each shape In Worksheets("Sheet1").Shapes
shape.Delete
Next
' loop thru the array and put images into columns A and resize the column, images and rows
For i = 0 To UBound(DirectoryList)
for j = 0 to 1
Debug.Print DirectoryList(i)
With Worksheets("Sheet1").Cells(i+1, j+1)
Set File = Worksheets("Sheet1").Pictures.Insert(DirectoryList(i))
File.Top = .Top
File.Left = .Left
File.ShapeRange.LockAspectRatio = msoFalse
File.Placement = xlMoveAndSize
.ColumnWidth = 30
.RowHeight = 100
File.ShapeRange.Width = 170
File.ShapeRange.Height = 100
End With
next j
Next i
lblExit:
Exit Sub
Erro:
MsgBox "OOpssie, Fail!", vbCritical, "Error"
End Sub
我希望这样(考虑到文件夹只有4个图像):
我得到这个:
考虑要点:
1.最后一个图像已更改,因为我不小心删除了它(这不会干扰问题)
1.没有办法告诉多少图像文件夹将有
1条答案
按热度按时间vh0rcniy1#
请尝试以下操作: