我有一个代码,允许我搜索文件路径和多个子文件夹来打开文件但是我希望能够打开多个名称略有不同的文件,例如effect00001.dat或effect00014.dat但是我我不太清楚如何
我的代码是:
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
答案 0 :(得分:3)
替换
行If CurrFile.Name = MyFile Then
通过
If CurrFile.Name Like MyFile Then
然后,您可以将wildcards用于MyFile
修改强>
我也认为该行
Set wb = Workbooks.Open(subfolders.Path & "\" & MyFile)
应替换为
Workbooks.Open(subfolders.Path & "\" & MyFile)
因为wb
值会立即被另一个值替换,而不会被使用。