问题
So far, I have been able to get the code to have all files opened in sub folders of the same name. The sub folder common name is June 2015, in the main "acquisitions" directory.
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
'I want to put code here
End If
Next
End If
End Sub
As can be seen, I was given this code by a kind person, but there was no comments on it. I'm quite new to VBA, and I'm having trouble figuring out why there are two different subs and what everything does.
I keep running into a problem where I can't perform any operations on the "File" after opening it. Any idea what could be causing this? For instance, there is a "type mismatch" error if I place Workbooks(File).Activate after "File" is opened. (This is just an example, as I am aware there are very few circumstance where .Activate should be used.)
回答1:
The problem is File contains the full path of the file, not just the file name. You will need to extract the file name only if you want to reference it from the Workbooks Excel object:
If Right(File, 4) = "xlsx" Then
Workbooks.Open fileName:=File
'I want to put code here
Dim fileName As String
fileName = CreateObject("Scripting.FileSystemObject").GetFileName(File)
Workbooks(fileName).Activate
' ... Do other stuff ...
End If
回答2:
Added some comments to the code, to try to help.
As for working with the opened workbook, I would place Dim wb as Workbook at the top of the DoFolder sub and replace the line Workbooks.Open Filename:=File with Set wb = Workbooks.Open(Filename:=File) Now wb is a workbook object, on which you can call wb.Activate or wb.Worksheets etc.
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) ' Declaring your variables Dim SubFolder Dim strName As String Dim pos As Integer ' Looping through your folders recursively ' We go into each folder until we find a folder with only files For Each SubFolder In Folder.SubFolders DoFolder SubFolder Next ' If we find only files or we have already opened all of the subfolders Dim File strName = Folder.name '<-- Store the folder name ' Find the string "June 2015" in the folder name and store the ' starting location as "pos" pos = InStr(strName, "June 2015") If pos > 0 Then '<-- If we found "June 2015"... For Each File In Folder.Files' <-- Go through each file If Right(File, 4) = "xlsx" Then '<-- if it's excel Workbooks.Open Filename:=File ' open it 'I want to put code here End If Next End If End Sub
Chip Pearson has some good guides for VBA:
来源:https://stackoverflow.com/questions/34420123/open-multiple-files-in-subfolders