我目前已经编码了如何打开某个文件夹中的所有文件
Dim MyFolder As String
Dim MyFile As String
MyFolder = "K:\Data Directories\Acquisitions"
MyFile = Dir(MyFolder & "\*.xlsx")
Do While Len(MyFile) > 0
Workbooks.Open FileName:=MyFolder & "\" & MyFile
MyFile = Dir
Loop
现在我尝试打开多个同名文件夹中的所有文件。
例如:
Dim MyFolder As String
Dim MyFile As String
Dim MyFolder2 As String
MyFolder = "K:\Data Directories\Acquisitions"
MyFolder2 = MyFolder & "*\June 2015"
MyFile = Dir(MyFolder2 & "\*.xlsx")
Do While Len(MyFile) > 0
Workbooks.Open FileName:=MyFolder2 & "\" & MyFile
MyFile = Dir
Loop
问题是我在2015年6月之前放置的问题。它是一个真实的" *"在路径代码中而不是通配符。
代码用于选择Acquisition目录中的所有文件夹,然后在其中查找2015年6月的文件夹。从那里,应该打开这些多个2015年6月文件夹中的所有Excel文件。我做错了什么?
答案 0 :(得分:1)
我认为这会做你想要的。试一试,看看会发生什么。
Sub DoFolderPart1()
Dim FileSystem As Object
Dim HostFolder As String
HostFolder = "K:\Data Directories\Acquisitions"
Set FileSystem = CreateObject("Scripting.FileSystemObject")
DoFolder FileSystem.GetFolder(HostFolder)
End Sub
Sub DoFolder(Folder)
Dim SubFolder
Dim strName As String
Dim pos As Integer
For Each SubFolder In Folder.SubFolders
DoFolder SubFolder
Next
Dim File
strName = Folder.name
pos = InStr(strName, "June 2015")
If pos > 0 Then
For Each File In Folder.Files
If Right(File, 4) = "xlsx" Then
Workbooks.Open Filename:=File
End If
Next
End If
End Sub