vba 集合最大大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3136507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 11:47:45  来源:igfitidea点击:

Collection Maximum Size

excelvbacollectionsexcel-vbamemory-limit

提问by l--''''''---------''''''''''''

Here's my code:

这是我的代码:

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

I am filling up a Collection with a list of filenames in a directory structure.

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

I have 2000 files, but the Collection only return 256. Does anyone know if there is a maximum number that it won't go past?

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

If so, can you please suggest a better way to code this macro so that it captures all 2000 files?

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

回答by Dr. belisarius

The code is working OK in Excel 2007. Perhaps what is happening is that you are trying to Watch the Collection in Debug mode. The Debugger shows only the first 256 items.

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

回答by Fionnuala

How about a disconnected recordset? This thread is about VBScript, but it is very similar to VBA:

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

How do I sort arrays using vbscript?

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