Excel - 计算特定的命名文件夹

时间:2015-02-16 04:11:35

标签: excel vba excel-vba count directory

我不是很擅长VBA。我正在尝试创建一个函数来计算以3开头的文件夹中有多少个子文件夹,例如:

C:\Files\ <This is the main folder>

如果只有一个名为32156的子文件夹,则会返回1

的结果

我发现很多脚本都会对子文件夹进行计数,但还不够熟悉它们。

1 个答案:

答案 0 :(得分:2)

试试这个:

    Sub TestCalling()
        MsgBox fGetFolderCount("C:\Files", "3")
    End Sub



    Function fGetFolderCount(ByVal FolderPath As String, Optional ByVal Prefix As String = vbNullString) As Long

        Dim D As Variant
        Dim C As Long
        D = Dir(FolderPath & Application.PathSeparator & Prefix & "*", vbDirectory)

        While D <> ""
            If Left(D, 1) <> "." Then
                C = C + 1
            End If
            D = Dir
        Wend

        fGetFolderCount = C

    End Function