Vba 显示文件夹属性:";包括「;

Vba 显示文件夹属性:";包括「;,vba,excel,Vba,Excel,我使用下面的代码来计算文件夹中的文件数。但是,如果有超过500个文件,则很难循环遍历每个文件。因此,我的问题是,是否可以获取文件夹属性-“Contains”,而不是遍历每个文件 Sub pdfcount() Dim FolderPath As String, path As String, count As Integer FolderPath = "C:\Documents and Settings\FPY\" path = FolderPath &"\*.pd

我使用下面的代码来计算文件夹中的文件数。但是,如果有超过500个文件,则很难循环遍历每个文件。因此,我的问题是,是否可以获取文件夹属性-“Contains”,而不是遍历每个文件

Sub pdfcount()
    Dim FolderPath As String, path As String, count As Integer
    FolderPath = "C:\Documents and Settings\FPY\"
    path = FolderPath &"\*.pdf"
    Filename = Dir(path)
    Do While Filename <>""
        count = count +1
        Filename = Dir()
    Loop
    Range("A1").Value = count
End Sub
子pdfcount()
Dim FolderPath为字符串,path为字符串,计数为整数
FolderPath=“C:\Documents and Settings\FPY\”
path=FolderPath&“\*.pdf”
Filename=Dir(路径)
文件名“”时执行此操作
计数=计数+1
Filename=Dir()
环
范围(“A1”)。值=计数
端接头

下面的函数将返回文件夹中的文件数

Function CountFiles(folderPath As String) As Long

    Dim fso As Object
    Dim files As Object

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set files = fso.GetFolder(folderPath).Files

    CountFiles = files.Count

End Function

谢谢@Gareth,太快了:)哎呀!!现在再次检查。如果我错了,请纠正我,不过OP似乎想计算文件夹中出现了多少PDF;你不能将通配符传递到fso文件路径?@luke\t你是对的。我想数一数pdf文件,刚刚意识到如果有子文件夹,Gareth提供的答案将不起作用。@Gareth因为这里不清楚,所以folderpath也包含子文件夹。@Neelesh你最初计算文件的方式是最快的,据我所知。我不相信有任何内置的方法来处理文件夹中的过滤文件。