5

这是我的代码:

Sub isdofsodjisf48023jroi23f984444444jiodfiosj12348023jroi23f98()


Dim colFiles As New Collection
    RecursiveDir colFiles, "C:\Documents and Settings\Alex Gordon\Desktop\testing\files\", "*.xls", True

    Dim vFile As Variant
    For Each vFile In colFiles
        Call writeincells(vFile)
    Next vFile

End Sub



Public Function RecursiveDir(colFiles As Collection, _
                             strFolder As String, _
                             strFileSpec As String, _
                             bIncludeSubfolders As Boolean)

    Dim strTemp As String
    Dim colFolders As New Collection
    Dim vFolderName As Variant

    'Add files in strFolder matching strFileSpec to colFiles
    strFolder = TrailingSlash(strFolder)
    strTemp = Dir(strFolder & strFileSpec)
    Do While strTemp <> vbNullString
        colFiles.Add strFolder & strTemp
        strTemp = Dir
    Loop

    If bIncludeSubfolders Then
        'Fill colFolders with list of subdirectories of strFolder
        strTemp = Dir(strFolder, vbDirectory)
        Do While strTemp <> vbNullString
            If (strTemp <> ".") And (strTemp <> "..") Then
                If (GetAttr(strFolder & strTemp) And vbDirectory) <> 0 Then
                    colFolders.Add strTemp
                End If
            End If
            strTemp = Dir
        Loop

        'Call RecursiveDir for each subfolder in colFolders
        For Each vFolderName In colFolders
            Call RecursiveDir(colFiles, strFolder & vFolderName, strFileSpec, True)
        Next vFolderName
    End If

End Function


Public Function TrailingSlash(strFolder As String) As String
    If Len(strFolder) > 0 Then
        If Right(strFolder, 1) = "\" Then
            TrailingSlash = strFolder
        Else
            TrailingSlash = strFolder & "\"
        End If
    End If
End Function

我正在用目录结构中的文件名列表填充一个集合。

我有 2000 个文件,但 Collection 只返回 256 个。有谁知道是否有一个不会超过的最大数量?

如果是这样,您能否建议一种更好的方法来编写此宏以捕获所有 2000 个文件?

4

2 回答 2

22

该代码在 Excel 2007 中运行良好。也许正在发生的事情是您正试图在调试模式下观看集合。调试器仅显示前 256 个项目。

于 2010-06-28T22:36:21.497 回答
1

断开连接的记录集怎么样?这个线程是关于 VBScript 的,但它与 VBA 非常相似:

如何使用 vbscript 对数组进行排序?

于 2010-06-28T22:38:21.967 回答