[英]VBA loop through subfolders to open multiple files
I have a code that allows me to search through a file path and multiple subfolders to open a file however I want to be able to open multiple files which have a slightly different name eg effect00001.dat or effect00014.dat however I'm not too sure how 我有一个代码,可以搜索文件路径和多个子文件夹来打开文件,但是我希望能够打开名称稍有不同的多个文件,例如effect00001.dat或effect00014.dat,但是我不太喜欢确定如何
My code is: 我的代码是:
Sub LoopSubfoldersAndFiles()
Dim fso As Object
Dim Folder As Object
Dim subfolders As Object
Dim MyFile As String
Dim wb As Workbook
Dim CurrFile As Object
With Application
.ScreenUpdating = False
.EnableEvents = False
.Calculation = xlCalculationManual
End With
Set fso = CreateObject("Scripting.FileSystemObject")
Set Folder = fso.GetFolder("\\My Documents\Output files\analysis-tool-development")
Set subfolders = Folder.subfolders
MyFile = "effect00001.dat"
For Each subfolders In subfolders
Set CurrFile = subfolders.Files
For Each CurrFile In CurrFile
If CurrFile.Name = MyFile Then
Set wb = Workbooks.Open(subfolders.Path & "\" & MyFile)
End If
Next
Next
Set fso = Nothing
Set Folder = Nothing
Set subfolders = Nothing
With Application
.EnableEvents = True
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
End With
End Sub
replace the line 更换线
If CurrFile.Name = MyFile Then
by 通过
If CurrFile.Name Like MyFile Then
You can then use wildcards for MyFile
然后,您可以对
MyFile
使用通配符
Edit: 编辑:
I also think the line 我也认为
Set wb = Workbooks.Open(subfolders.Path & "\" & MyFile)
should be replaced by 应该替换为
Workbooks.Open(subfolders.Path & "\" & MyFile)
since wb
value is replaced by another one immediately, and not used. 因为
wb
值会立即被另一个替换,因此不会使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.