Vba 在用户选择的特定年份的文件夹中循环
我有几个名为“*2016-*2017-*2018”的文件夹,我想让用户可以使用输入框选择年份(作为字符串),并根据输入框浏览用户选择了年份但没有进入循环的文件夹。在说明中,以文件夹的排列方式和MCVE为例Vba 在用户选择的特定年份的文件夹中循环,vba,excel,Vba,Excel,我有几个名为“*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 Objec
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
您可以使用:
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
您当前的代码有什么问题?它永远不会进入循环,
如果f1.Name像首秀或f1.Name像fin那么
不工作调试应用程序时f1.Name的值是什么?你确定你的Like
是正确的吗(我看不到任何通配符,那么为什么要使用Like
)?我认为它不会进入循环的唯一情况是没有子文件夹。我刚刚用我电脑上的一个文件夹对它进行了测试,它进入了循环。如果文件夹是2018文件夹,我将如何获得第一部分,与CLng(Split(f1.Name,“-”)(1))
相对,它在哪里获得最后一部分请参见编辑后的答案。如果它解决了你的问题,请考虑将它标记为接受。非常感谢。
folderYear = CLng(Split(f1.Name, "-")(0))