Excel 在子文件夹中循环时,如何跳过新创建的文件夹?
我从以前的文章中拼凑了很多代码(感谢大家!),我几乎有了一个可行的解决方案。 我希望发生的是:Excel 在子文件夹中循环时,如何跳过新创建的文件夹?,excel,vba,Excel,Vba,我从以前的文章中拼凑了很多代码(感谢大家!),我几乎有了一个可行的解决方案。 我希望发生的是: 用户选择一个文件夹 将在该文件夹内创建一个新文件夹,并将一些.dwg文件移动到其中 然后,代码深入到下一个文件夹并执行相同的操作 我的问题是,代码正在深入到新创建的文件夹中,并创建一个无休止的循环。有没有办法跳过我刚刚创建的文件夹?该文件夹始终命名为“Original DWGs DD mm yy”,因此我正在考虑添加 If InStr(FromPath, "original") = 0 Then
- 用户选择一个文件夹
- 将在该文件夹内创建一个新文件夹,并将一些.dwg文件移动到其中
- 然后,代码深入到下一个文件夹并执行相同的操作
If InStr(FromPath, "original") = 0 Then
Exit Sub
End If
但我不认为在fso循环中“退出sub”是正确的做法
Option Explicit
Dim sFolder As String
Sub CommandButton1_Click()
' Open the select folder prompt
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show = -1 Then ' if OK is pressed
sFolder = .SelectedItems(1)
End If
End With
If sFolder <> "" Then ' if a file was chosen
Debug.Print sFolder
End If
DrillDown
End Sub
Sub DrillDown()
Dim FSO As Object 'FileSystemObject
Dim fldStart As Object 'Folder
Dim fld As Object 'Folder
Dim fl As Object 'File
Dim Mask As String
Set FSO = CreateObject("scripting.FileSystemObject") ' late binding
Set fldStart = FSO.GetFolder(sFolder) ' <-- use your FileDialog code here
Mask = "*.dwg"
For Each fld In fldStart.SubFolders
ListFolders fld, Mask
Next
End Sub
Sub ListFolders(fldStart As Object, Mask As String)
Dim fld As Object 'Folder
Dim FromPath As String
For Each fld In fldStart.SubFolders
Debug.Print fld.Path & "\"
'move all specified files from FromPath to ToPath.
'Note: It will create the folder ToPath for you
Dim FSO As Object
Dim ToPath As String
Dim FileExt As String
Dim FNames As String
Dim diaFolder As FileDialog
Dim selected As Boolean
Dim FldCheck As String
FromPath = fld.Path & "\"
ToPath = FromPath & "Original DWGs " & Format(Date, "dd-mm-yy") '<< Change only the destination folder
Debug.Print ToPath
FileExt = "*.dwg" '<< Change
If Right(FromPath, 1) <> "\" Then
FromPath = FromPath & "\"
End If
FNames = Dir(FromPath & FileExt)
If Len(FNames) = 0 Then
MsgBox "No .dwg files in " & FromPath
'Exit Sub
GoTo Err
End If
Set FSO = CreateObject("scripting.filesystemobject")
If FSO.FolderExists(ToPath) = False Then
FSO.CreateFolder (ToPath)
End If
FSO.MoveFile Source:=FromPath & FileExt, Destination:=ToPath
Err:
FileExt = "*.err" '<< Change
If Right(FromPath, 1) <> "\" Then
FromPath = FromPath & "\"
End If
FNames = Dir(FromPath & FileExt)
If Len(FNames) = 0 Then
'MsgBox "No .err files in " & FromPath
'Exit Sub
GoTo Bak
End If
Set FSO = CreateObject("scripting.filesystemobject")
If FSO.FolderExists(ToPath) = False Then
FSO.CreateFolder (ToPath)
End If
FSO.MoveFile Source:=FromPath & FileExt, Destination:=ToPath
'---
Bak:
FileExt = "*.bak" '<< Change
If Right(FromPath, 1) <> "\" Then
FromPath = FromPath & "\"
End If
FNames = Dir(FromPath & FileExt)
If Len(FNames) = 0 Then
'MsgBox "No .bak files in " & FromPath
'Exit Sub
GoTo Log
End If
Set FSO = CreateObject("scripting.filesystemobject")
If FSO.FolderExists(ToPath) = False Then
FSO.CreateFolder (ToPath)
End If
FSO.MoveFile Source:=FromPath & FileExt, Destination:=ToPath
'---
Log:
FileExt = "*.log" '<< Change
If Right(FromPath, 1) <> "\" Then
FromPath = FromPath & "\"
End If
FNames = Dir(FromPath & FileExt)
If Len(FNames) = 0 Then
'MsgBox "No .log files in " & FromPath
Exit Sub
End If
Set FSO = CreateObject("scripting.filesystemobject")
If FSO.FolderExists(ToPath) = False Then
FSO.CreateFolder (ToPath)
End If
FSO.MoveFile Source:=FromPath & FileExt, Destination:=ToPath
Set diaFolder = Nothing
ListFolders fld, Mask
Next
End Sub
选项显式
作为字符串的Dim-sFolder
子命令按钮1_单击()
'打开“选择文件夹”提示
使用Application.FileDialog(msoFileDialogFolderPicker)
如果.Show=-1,则“如果按OK”
sFolder=.SelectedItems(1)
如果结束
以
如果选择了文件,则为文件夹“”,然后为“”
调试.打印文件夹
如果结束
深入调查
端接头
子向下钻取()
将FSO设置为对象的FileSystemObject
将fldStart设置为对象文件夹
Dim fld作为对象文件夹
Dim fl作为“对象”文件
将遮罩变暗为字符串
设置FSO=CreateObject(“scripting.FileSystemObject”)“后期绑定
设置fldStart=FSO.GetFolder(sFolder)“在向下展开中,您应该在循环子文件夹的位置添加您提到的检查:
For Each fld In fldStart.SubFolders
If InStr(1, fld.Name, "Original DWGs ") = 0 Then ListFolders fld, Mask
Next
For Each fld In fldStart.SubFolders
If InStr(1, fld.Name, "Original DWGs ") = 0 Then ListFolders fld, Mask
Next