以下是使用VBA遍历文件夹内包含的文件数量的示例代码:

Sub countFilesInFolder()
    Dim folderPath As String
    Dim filesCount As Integer
    folderPath = "C:\Users\username\Desktop\FolderName\" '更改为您要遍历的文件夹路径
    filesCount = 0 '计数器
    If Right(folderPath, 1) <> "\" Then folderPath = folderPath & "\"
    If Dir(folderPath, vbDirectory) <> vbNullString Then
        Dim fileName As String
        fileName = Dir(folderPath & "*.*", vbNormal)
        Do Until fileName = vbNullString
            If Left(fileName, 1) <> "." Then filesCount = filesCount + 1 '忽略隐藏文件
            fileName = Dir()
        Loop
    End If
    MsgBox "文件夹" & folderPath & "内包含 " & filesCount & " 个文件。"
End Sub

您可以将folderPath变量更改为您要遍历的文件夹路径。该代码将计算该文件夹中的所有文件的数量,并在消息框中显示结果。请注意,此代码不会递归遍历子文件夹中的文件

如何使用VBA遍历文件夹内包含的文件数量

原文地址: https://www.cveoy.top/t/topic/fpgu 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录