Arrays 查找递归文件并保存在动态数组(VBA)中
我想在VBA中使用动态数组来保存找到的字符串,并使用Arrays 查找递归文件并保存在动态数组(VBA)中,arrays,vba,Arrays,Vba,我想在VBA中使用动态数组来保存找到的字符串,并使用FileSystemObject来查找文件 我目前使用的代码就是这个 Private Sub cmdStartSearch_Click() Dim resultList(0) As String Call FindFile(resultList, ".png", "Q:\", True) End Sub Private Sub FindFile(ByRef resultList() As String, target As S
FileSystemObject
来查找文件
我目前使用的代码就是这个
Private Sub cmdStartSearch_Click()
Dim resultList(0) As String
Call FindFile(resultList, ".png", "Q:\", True)
End Sub
Private Sub FindFile(ByRef resultList() As String, target As String, ByVal aPath As String, useSubfolders As Boolean)
Dim myFileSystemObject As FileSystemObject, curFolder As folder, folder As folder
Dim folColl As Folders, file As file, fileColl As Files
Set myFileSystemObject = New FileSystemObject
Set curFolder = myFileSystemObject.GetFolder(aPath)
Set folderList = curFolder.SubFolders
Set fileList = curFolder.Files
For Each file In fileList
ReDim Preserve resultList(1 To UBound(resultList) + 1) As String
If InStr(file.Name, target) > 0 Then
resultList(UBound(resultList)) = file.Name
Debug.Print file.Name
End If
Next
If useSubfolders Then
For Each folder In folderList
DoEvents 'Yield execution so other events may be processed
If Not foundTarget Then
FindFile resultList, target, folder.Path, useSubfolders
End If
Next
End If
Set myFileSystemObject = Nothing
Set curFolder = Nothing
Set folderList = Nothing
Set fileList = Nothing
End Sub
但是,如果阵列已修复或临时锁定,则此操作将失败
我怎样才能绕过这个问题,或者解决原来的问题呢?我觉得这是一个更好/更简单的解决方案。您要做的是像以前一样循环遍历文件列表,然后返回一个带有文件名的长字符串,最后使用Split函数将其打断,这将生成一个字符串数组 您的代码可以简单地更改为
Private Sub cmdStartSearch_Click()
Dim resultList() As String
resultList = Split(FindFile(".png", "Q:\", True), ";")
End Sub
Private Function FindFile(target As String, ByVal aPath As String, useSubfolders As Boolean) As String
Dim retStr As String
Dim myFileSystemObject As FileSystemObject, curFolder As folder, folder As folder
Dim folColl As Folders, file As file, fileColl As Files
Set myFileSystemObject = New FileSystemObject
Set curFolder = myFileSystemObject.GetFolder(aPath)
Set folderList = curFolder.SubFolders
Set fileList = curFolder.Files
For Each file In fileList
If InStr(file.Name, target) > 0 Then
retStr = retStr & ";" & file.Name
Debug.Print file.Name
End If
Next
If useSubfolders Then
For Each folder In folderList
DoEvents 'Yield execution so other events may be processed
If Not foundTarget Then
retStr = retStr & ";" & FindFile(target, folder.Path, useSubfolders)
End If
Next
End If
Set myFileSystemObject = Nothing
Set curFolder = Nothing
Set folderList = Nothing
Set fileList = Nothing
If Len(retStr) > 0 Then retStr = Right(retStr, Len(retStr)-1)
FindFile = retStr
End Function
为什么变量folderList、fileList、foundTarget没有在上下文中声明?将
UBound(resultList)+1
更改为UBound(resultList)
UBound
不是基于0的。@JLILIAman,你能解释一下UBound不是基于0的意思吗,请.Ubound将计算包括resutlList的第0个下标在内的下标,因此不需要添加1。@JLILIAman,因此如果数组包含arr(0)=76,arr(1)=77。UBound(arr)将产生2或1?根据您的描述,它应该返回2,因为0计为1,1计为2。然而,我认为答案应该是1,而不是2。这似乎不是实际问题。我现在将列表定义为未初始化的Dim resultList()字符串
,但现在错误下标超出范围
代码主要是复制的,所以我不知道为什么没有定义一些变量。一般来说,您的方法在vba中常见吗?我觉得它很有用,但奇怪的是,使用数组是如此复杂。VBA中的数组有点棘手,但这个解决方案可能比您希望的方式简单得多!Split是一种非常常用的返回字符串数组的方法。