查找编号最大的子文件夹

时间:2017-03-14 16:27:50

标签: excel-vba vba excel

我有一个包含很多子文件夹的文件夹,它们是基于一个带有迭代编号的模式命名的,用于98%的文件夹。

我想在其中找到最高编号(子文件夹名称,maxNumber = ??),该怎么做?

numOfRows = maxFolder- 32020

'loops through each file in the directory and prints their names and path
'For Each objSubFolder In objFolder.subfolders to slow....
 for pnr  = 32020 to maxFolder
    DoEvents

    Call ProgressBar.setMessage("Updating for .." & pnr, ((i + 1) / (numOfRows + 1)) * 100)

1 个答案:

答案 0 :(得分:1)

您想要查找具有最多数量的子文件夹吗?这可能是一个开始的好地方。

http://www.learnexcelmacro.com/wp/download/

这是脚本。

Sub GetFilesInFolder(SourceFolderName As String, Subfolders As Boolean)

'--- For Example:Folder Name= "D:\Folder Name\" and Flag as Yes or No

Dim FSO As Scripting.FileSystemObject
Dim SourceFolder As Scripting.folder, SubFolder As Scripting.folder
Dim FileItem As Scripting.File
'Dim r As Long
    Set FSO = New Scripting.FileSystemObject
    Set SourceFolder = FSO.GetFolder(SourceFolderName)

    '--- This is for displaying, whereever you want can be configured

    r = 14
    For Each FileItem In SourceFolder.Files
        Cells(r, 2).Formula = r - 13
        Cells(r, 3).Formula = FileItem.Name
        Cells(r, 4).Formula = FileItem.Path
        Cells(r, 5).Formula = FileItem.Size
        Cells(r, 6).Formula = FileItem.Type
        Cells(r, 7).Formula = FileItem.DateLastModified
        Cells(r, 8).Formula = "=HYPERLINK(""" & FileItem.Path & """,""" & "Click Here to Open" & """)"

        r = r + 1   ' next row number
    Next FileItem

    '--- This is the Function to go each and Every Folder and get the Files. This is a Nested-Function Calling.

    If Subfolders = True Then
        For Each SubFolder In SourceFolder.Subfolders
            ListFilesInFolder SubFolder.Path, True
        Next SubFolder
    End If

    Set FileItem = Nothing
    Set SourceFolder = Nothing
    Set FSO = Nothing
End Sub

最后,在上面的链接中,您可以下载一个名为“文件管理器”的示例文件;单击“立即下载”以获取该文件。那应该做你想要的。

相关问题