首页 > 解决方案 > Sub 循环时保持数组值

问题描述

我目前有这段代码可以找到所有文件和文件夹并将其写入表中。问题是它有时很慢。

下面的代码已修改,以便写入数组,但在代码循环时传递数组时遇到问题。

最终,我希望将数组传递给第一个 sub,以便我可以立即将其转置到表中。

Sub FileAndFolder()

Dim FSOLibrary As Object
Dim FSOFolder As Object
Dim FolderName As String
Dim FilesTbl As ListObject
Set FilesTbl = Range("FilesTbl").ListObject

'Set the folder name to a variable
FolderName = Left$(ActiveWorkbook.Path, InStrRev(ActiveWorkbook.Path, "\"))

'Set the reference to the FSO Library
Set FSOLibrary = CreateObject("Scripting.FileSystemObject")

'Another Macro must call LoopAllSubFolders Macro to start
LoopAllFolders FSOLibrary.GetFolder(FolderName)

'return TempArray here and paste into table

'Range(FilesTbl.ListColumns("File Name").DataBodyRange(1)) = TempArray

End Sub

Sub LoopAllFolders(FSOFolder As Object)
'Don’t run the following macro, it will be called from the macro above

Dim FSOSubFolder As Object
Dim FSOFile As Object
Dim FolderPath As String
Dim FileName As String
Dim TempArray() As String

'For each subfolder call the macro
For Each FSOSubFolder In FSOFolder.SubFolders
    LoopAllFolders FSOSubFolder
Next

'For each file, print the name
For Each FSOFile In FSOFolder.Files

    'Insert the actions to be performed on each file
    FileName = FSOFile.Name
    FolderPath = FSOFile.ParentFolder
          
    If Left(FileName, 2) = "~$" Then GoTo NEXTINLOOP
    ReDim Preserve TempArray(0 To 3, 0 To i)
        
    TempArray(0, i) = FileName
    TempArray(1, i) = FolderPath & "\" & FileName 'file
    TempArray(2, i) = FolderPath 'folder
    TempArray(3, i) = FolderPath & "\" & FileName 'showpath
        
    i = i + 1
NEXTINLOOP:
Next
 
End Sub 'TempArray and i clears here

谢谢。

标签: arraysexcelvbamultidimensional-array

解决方案


您要么需要在模块级别声明一个变量,以便模块中的所有方法都可以使用文件夹信息列表,要么将“LoopAllFolders”更改为函数,以便您可以返回已整理的信息。

下面的函数将返回一个 Variant,其中包含一个数组数组(通常称为锯齿数组)。您可以使用此命名法访问锯齿状数组

Varname(x)(y)

您将需要调用方法中的一个变量来接收锯齿状数组

例如

Dim myFileInfo as Variant
MyFileInfo = LoopAllFolders(FSOLibrary.GetFolder(FolderName))

这是更新的功能

Public Function LoopAllFolders(FSOFolder As Scripting.FileSystemObject) As Variant
'Don’t run the following macro, it will be called from the macro above

    Dim FileInfo As Scripting.Dictionary: Set myFileInfo = New Scripting.Dictionary

'For each subfolder call the macro

    Dim FSOSubFolder As Scripting.Folder
    For Each FSOSubFolder In FSOFolder.SubFolders
        LoopAllFolders FSOSubFolder
    Next

    'For each file, print the name
    Dim FSOFile As Scripting.File
    For Each FSOFile In FSOFolder.Files

        'Insert the actions to be performed on each file
        Dim FileName As String
        FileName = FSOFile.Name
    
        Dim FolderPath As String
        FolderPath = FSOFile.ParentFolder
          
        If Not Left(FileName, 2) = "~$" Then
    
            myFileInfo.Add Array(FileName, FolderPath & "\" & FileName, FolderPath, FolderPath & "\" & FileName)
        
        End If
    
    Next

    LoopAllFolders = myFileInfo.Items
 
End Function

上面的代码可能并不完美,但至少它为您指明了正确的方向。

根据您的问题,您可能会通过 VBA 教程做得很好,因为函数是相当基础的,如果您不知道它们......

为了在您的旅程中为您提供帮助,我还建议您安装出色且免费的 RubberDuck 插件。


推荐阅读