Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel计算包含子文件夹的文件夹中的文件时出错_Excel_Vba - Fatal编程技术网

Excel计算包含子文件夹的文件夹中的文件时出错

Excel计算包含子文件夹的文件夹中的文件时出错,excel,vba,Excel,Vba,它适用于单个文件夹,但如果树级别高于2,则错误会随着树的长度而增加 我试图在一个包含子文件夹的文件夹中获得xl的数量,以便创建一个数组 (我将使用“打开另一个子系统中的所有文件”并创建报告) 有什么想法吗 Private Sub Countfiles(FF As Scripting.Folder) Dim F As Scripting.file Dim SubF As Scripting.Folder Dim k As Integer

它适用于单个文件夹,但如果树级别高于2,则错误会随着树的长度而增加

我试图在一个包含子文件夹的文件夹中获得xl的数量,以便创建一个数组

(我将使用“打开另一个子系统中的所有文件”并创建报告)

有什么想法吗

Private Sub Countfiles(FF As Scripting.Folder)
        Dim F As Scripting.file
        Dim SubF As Scripting.Folder
        Dim k As Integer

        For Each F In FF.Files
                If F.Path Like "*.xl*" Then
                        k = k + 1
                        Debug.Print r_tot + k
                Else
                End If
        Next

        For Each SubF In FF.Subfolders
                r_tot = r_tot + k
                Countfiles SubF
        Next SubF

End Sub

使用以下代码段获取单个文件夹中的文件计数(Excel类型.xls)。将其应用于所有感兴趣的文件夹,并将计数添加到累加器变量

Function FilesCount(FolderPath)
    Dim FolderPath As String, path As String, count As Integer

    path = FolderPath & "\*.xls"
    Filename = Dir(path)

    Do While Filename <> ""
       count = count + 1
       Filename = Dir()
    Loop

    FilesCount=count
End Sub
函数文件计数(FolderPath)
Dim FolderPath为字符串,path为字符串,计数为整数
path=FolderPath&“\*.xls”
Filename=Dir(路径)
文件名“”时执行此操作
计数=计数+1
Filename=Dir()
环
filescont=count
端接头

希望这会有所帮助。Rgds,

循环的每个迭代都有自己的
k
实例,您不会将其值返回给调用例程

您可以将计数作为参数添加到
Sub

Private Sub Countfiles(FF As Scripting.Folder, ByRef count As Long)
    Dim f As Scripting.file
    Dim SubF As Scripting.Folder

    For Each f In FF.Files
        If f.Path Like "*.xl*" Then
            count = count + 1
        End If
    Next

    For Each SubF In FF.Subfolders
        Countfiles SubF, count
    Next SubF   

End Sub
或者将其转换为
功能

Function fCountfiles(FF As Scripting.Folder) As Long
    Dim count As Long       
    Dim f As Scripting.file
    Dim SubF As Scripting.Folder

    For Each f In FF.Files
        If f.Path Like "*.xl*" Then
            count = count + 1
        End If
    Next

    For Each SubF In FF.Subfolders
        count = count + fCountfiles(SubF)
    Next SubF
    fCountfiles = count
End Function

另一种方法是通过
VBA
使用
PowerShell
——一行就足以递归地计算文件和文件夹的数量(或转储相同的完整列表)

下面的代码是对使用FSO或DIR的递归循环的极大改进,但与直接使用
PowerShell
的简单性相比,令人沮丧的是,直接从文件夹
C:\temp
中查看它只是:

写入主机(获取子项c:\temp-recurse-include*.xls*|测量对象|导出csv c:\temp\filename.csv

  • 更改
    strFolder
    ,为文件和文件夹的计数设置感兴趣的文件夹
代码


与使用递归函数不同,只需循环遍历文件夹/子文件夹,并像Rgds中显示的那样计数文件,@alexbell如果问题明显希望深入到一级以上的子文件夹,这将如何工作?
Sub BYpass()
Dim strFolder As String
Dim StrIn As String
Dim WB As Workbook
Dim X1
Dim X2
strFolder = "c:\temp"
StrIn = "C:\temp\filename.csv"
'export file count
X1 = Shell("powershell.exe Write-Host ( Get-ChildItem " & strFolder & " -recurse -include *.xls* | Measure-Object | export-csv " & StrIn & ")", 1)
'export detail of all files
X2 = Shell("powershell.exe Write-Host ( Get-ChildItem " & strFolder & " -recurse -include *.xls* | export-csv " & StrIn & ")", 1)
Set WB = Workbooks.Open(StrIn)
End Sub