为什么我的代码无法打开多个文件 vba
Why wont my code open multiple files vba
我有一个代码应该打开文件路径中所有名为 "effect00*" 的文件,但它只会打开它找到的第一个文件,但我希望它打开所有文件,有人知道为什么我的代码不会这样做?
我的代码是:
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 = "effect00*.dat"
For Each subfolders In subfolders
Set CurrFile = subfolders.Files
For Each CurrFile In CurrFile
If CurrFile.Name Like 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
结束子
查看您的声明:
:
For Each subfolders In subfolders
:
显然,subfolders
中只有一个对象subfolders
。
正如 Variatus 所建议的,尝试为您的变量实施更好的命名策略。
这里有很多过度 Set
的东西,它使阅读更容易,但大多是不必要的。例如,由于您不使用 Folder
对象而不是获取子文件夹,而不是:
Set Folder = fso.GetFolder("\My Documents\Output files\analysis-tool-development")
Set subfolders = Folder.subfolders
你可以:
Set subfolders = fso.GetFolder("\My Documents\Output files\analysis-tool-development").subfolders
但假设您想让它易于阅读等,我已经检查了代码并重新标记了您的对象等,以 a) 区分 vba 特定的措辞和 b) 识别 parent/child喜欢所有权:
Sub LoopSubfoldersAndFiles()
Dim fso As Object
Dim myTopFolder As Object
Dim mySubFolders As Object
Dim mySingleFolder As Object
Dim myFileCollection As Object
Dim mySingleFile As Object
Dim myFilePattern As String
Dim wb As Workbook
With Application
.ScreenUpdating = False
.EnableEvents = False
.Calculation = xlCalculationManual
End With
Set fso = CreateObject("Scripting.FileSystemObject")
Set myTopFolder = fso.GetFolder("\My Documents\Output files\analysis-tool-development")
Set mySubFolders = myTopFolder.subfolders
myFilePattern = "effect00*.dat"
For Each mySingleFolder In mySubFolders
Set myFileCollection = mySingleFolder.Files
For Each mySingleFile In myFileCollection
If mySingleFile.Name Like myFilePattern Then
Set wb = Workbooks.Open(mySingleFolder.Path & "\" & mySingleFile.Name)
End If
Next
Next
Set fso = Nothing
Set myTopFolder = Nothing
Set mySubFolders = Nothing
Set mySubFolders = Nothing
Set mySingleFolder = Nothing
Set myFileCollection = Nothing
Set mySingleFile = Nothing
With Application
.EnableEvents = True
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
End With
End Sub
最后,我把它们留在了里面,但是有一块 Set xxx = Nothing
许多人认为没有必要。它看起来 neat/tidy 但我记得在某处读到 End Sub
无论如何都会清除这些内容。
我有一个代码应该打开文件路径中所有名为 "effect00*" 的文件,但它只会打开它找到的第一个文件,但我希望它打开所有文件,有人知道为什么我的代码不会这样做?
我的代码是:
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 = "effect00*.dat"
For Each subfolders In subfolders
Set CurrFile = subfolders.Files
For Each CurrFile In CurrFile
If CurrFile.Name Like 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
结束子
查看您的声明:
:
For Each subfolders In subfolders
:
显然,subfolders
中只有一个对象subfolders
。
正如 Variatus 所建议的,尝试为您的变量实施更好的命名策略。
这里有很多过度 Set
的东西,它使阅读更容易,但大多是不必要的。例如,由于您不使用 Folder
对象而不是获取子文件夹,而不是:
Set Folder = fso.GetFolder("\My Documents\Output files\analysis-tool-development")
Set subfolders = Folder.subfolders
你可以:
Set subfolders = fso.GetFolder("\My Documents\Output files\analysis-tool-development").subfolders
但假设您想让它易于阅读等,我已经检查了代码并重新标记了您的对象等,以 a) 区分 vba 特定的措辞和 b) 识别 parent/child喜欢所有权:
Sub LoopSubfoldersAndFiles()
Dim fso As Object
Dim myTopFolder As Object
Dim mySubFolders As Object
Dim mySingleFolder As Object
Dim myFileCollection As Object
Dim mySingleFile As Object
Dim myFilePattern As String
Dim wb As Workbook
With Application
.ScreenUpdating = False
.EnableEvents = False
.Calculation = xlCalculationManual
End With
Set fso = CreateObject("Scripting.FileSystemObject")
Set myTopFolder = fso.GetFolder("\My Documents\Output files\analysis-tool-development")
Set mySubFolders = myTopFolder.subfolders
myFilePattern = "effect00*.dat"
For Each mySingleFolder In mySubFolders
Set myFileCollection = mySingleFolder.Files
For Each mySingleFile In myFileCollection
If mySingleFile.Name Like myFilePattern Then
Set wb = Workbooks.Open(mySingleFolder.Path & "\" & mySingleFile.Name)
End If
Next
Next
Set fso = Nothing
Set myTopFolder = Nothing
Set mySubFolders = Nothing
Set mySubFolders = Nothing
Set mySingleFolder = Nothing
Set myFileCollection = Nothing
Set mySingleFile = Nothing
With Application
.EnableEvents = True
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
End With
End Sub
最后,我把它们留在了里面,但是有一块 Set xxx = Nothing
许多人认为没有必要。它看起来 neat/tidy 但我记得在某处读到 End Sub
无论如何都会清除这些内容。