我想知道扩展名为.pdf的多个文件夹中有多少个文件。我阅读了带有消息框的数百个示例,但这不是我想要的。我只想将数字显示在excel中提供的每个路径旁边的单元格中。基本上,我需要一个循环,该循环从excel文件中选择路径,并从路径中获取计数,然后将计数粘贴到该路径的前面。
子样本()
Dim FolderPath As String, path As String, count As Integer
FolderPath = "C:\Documents and Settings\*"
path = FolderPath & "\*.pdf"
Filename = Dir(path)
Do While Filename <> ""
count = count + 1
Filename = Dir()
Loop
Range("A2").Value = count
'MsgBox count & " : files found in folder"
结束子
答案 0 :(得分:0)
演示下面的链接可能会对您有所帮助。
https://wellsr.com/vba/2018/excel/list-files-in-folder-and-subfolders-with-vba-filesystemobject/
答案 1 :(得分:0)
最简单的方法,只需五个步骤!
1)在VBA项目中,添加一个模块。
2)转到菜单>工具>引用,然后添加对Microsoft脚本运行时的引用
3)在创建的模块顶部添加一个变量
Dim fso As New Scripting.FileSystemObject
4)创建函数:
Public Function COUNTFILES(ByVal directory As String, ByVal ext As String) As Long
Dim total_files As Long
Dim i As File
If directory = "" Or ext = "" Then
COUNTFILES = 0
Exit Function
End If
For Each i In fso.GetFolder(directory).FILES
If InStr(1, i.Name, ext) > 0 Then
total_files = total_files + 1
End If
Next
COUNTFILES = total_files
End Function
5)现在,您可以在工作表中使用VBA函数,就像使用内置函数一样,在单元格中键入等号。
=COUNTFILES("THE CELL WHERE IS THE DIRETORY PATH",".pdf")