Excel VBA:如何提取名称中包含特定年份的子文件夹从“ 2020”开始的子文件夹并停在那棵树上

问题描述

尝试使下面的代码适应

  1. 显示以“ 2020”开头的子文件
  2. 仅向我显示该级别的子文件夹(因此现在不需要这些子文件夹中的子文件夹,因为这会循环并为我提供子文件夹中的子文件夹)

修复了以下拼写错误

解决方法

如果2020已经包含在子文件夹名称中,则可以尝试 剩下的功能...

rgds

,

'lists information about the folders in SourceFolder
' example: ListSubFolders "C:","2020*"
Sub ListSubFolders(SourceFolderName As String,Optional pattern as string="")  
    Dim FSO As Scripting.FileSystemObject 
    Dim sf As Scripting.Folder,c As Range

    Set c = sheets("Folders").Cells(rows.count,1).End(xlup).offset(1,0)
    Set FSO = New Scripting.FileSystemObject
    For Each sf In FSO.GetFolder(SourceFolderName).SubFolders
        If Len(pattern)=0 or SourceFolder.Name Like pattern Then
        ' display folder properties
        c.Resize(1,7).Value= Array( _
                     sf.Path,sf.Name,sf.Size,sf.SubFolders.Count,_
                     sf.Files.Count,sf.ShortName,sf.ShortPath)
        End If
        Set c = c.offset(1,0)
    Next SubFolder
End Sub