循环浏览用户选择的特定年份的文件夹

时间:2018-08-09 08:52:24

标签: vba excel-vba

我有几个文件夹的名称类似“ * 2016-* 2017- * 2018”,我想让用户可以通过输入框选择年份(以字符串形式),并根据浏览的文件夹由用户选择年份,但并没有进入循环。在描述中,有关如何布置文件夹和MCVE的示例

Option Explicit

'folder t2-2018
'folder t2-2017
'folder t1-2016
'
'
'
'
Public Sub picking()
Dim Fso As Object
Dim subf As Variant
Dim f1 As Object, f2 As Object
Dim debut As Variant
Dim fin
Set Fso = CreateObject("Scripting.FileSystemObject")
debut = Application.InputBox("Veuillez saisir l'année de début", "Début", , , , , , 2)
fin = Application.InputBox("Veuillez saisir l'année de fin", "Fin", , , , , , 2)
subf = "C:\Users\A60179\Desktop\Nouveau dossier"
    For Each f1 In Fso.GetFolder(subf).subfolders
        If f1.Name Like debut Or f1.Name Like fin Then
        msgbox ("nom" & f1.Name)
            For Each f2 In f1.Files
                If f2.Name Like "*cahier*" Then
                msgbox ("ok")
                Else
                msgbox ("not ok")
                End If
            Next f2
        Else
        End If
    Next f1
End Sub

1 个答案:

答案 0 :(得分:1)

您可以使用:

Dim debut As Long, fin As Long
Dim folderYear As Long

Set Fso = CreateObject("Scripting.FileSystemObject")
debut = Application.InputBox("Veuillez saisir l'année de début", "Début", , , , , , 2)
fin = Application.InputBox("Veuillez saisir l'année de fin", "Fin", , , , , , 2)
subf = "C:\Users\A60179\Desktop\Nouveau dossier"

For Each f1 In Fso.GetFolder(subf).SubFolders
    folderYear = CLng(Split(f1.Name, "-")(1)) ' get the last "part" of folder name
    If folderYear >= debut And folderYear <= fin Then

如果文件夹名称模式为“ year-folder”,则只需采用:

folderYear = CLng(Split(f1.Name, "-")(0))