Hello
bigme,
Thanks for sharing your problem with us. I understand that you want to open files with a target string from the sub-subfolders of a given folder. I have prepared a folder on my computer according to your specifications. The folder consists of subfolders and sub-subfolders. The following video will provide you with an overview of the folder.
Now, to open all files with a target string, I opened a workbook (from any location) >> went to the
Developer tab >> clicked the dropdown icon of
Insert option >> selected the
Command Button option from
ActiveX Controls.
I dragged my mouse pointer over the worksheet to draw the command button. Then I right-clicked over it and selected the Property option.
I changed the Caption to “Click here to open files from sub-folder” and Changed the Font style.
Afterward, right-click over the command button again and select the
View Code option.
Then, I inserted the following Code and Saved the Workbook.
Code:
Private Sub CommandButton1_Click()
Dim folder_path As String
Dim file_name As String
Dim file_extension As String
Dim target_string As String
Dim fso As Object
Dim source_folder As Object
Dim sub_folder As Object
Dim subsub_folder As Object
Dim file As Object
folder_path = "C:\Users\Seemanto\Desktop\FQ 212\Folder ABC"
target_string = InputBox("Enter a string for opening files:")
If target_string = "" Then
MsgBox "You did not enter anything"
Exit Sub
End If
Set fso = CreateObject("Scripting.FileSystemObject")
Set source_folder = fso.GetFolder(folder_path)
For Each sub_folder In source_folder.Subfolders
For Each sub_SubFolder In sub_folder.Subfolders
For Each file In sub_SubFolder.Files
If InStr(1, file.Name, target_string, vbTextCompare) > 0 Then
If InStr(1, file.Name, "xlsx", vbtexcompare) > 0 Or InStr(1, file.Name, "xlsm", vbTextCompare) > 0 Then
Workbooks.Open sub_SubFolder & "\" & file.Name
End If
End If
Next file
Next sub_SubFolder
Next sub_folder
Set fso = Nothing
Set source_folder = Nothing
Set sub_folder = Nothing
Set sub_SubFolder = Nothing
Set file = Nothing
End Sub
Now, close the workbook and re-open the workbook. Click on the
Command Button. An input box like the following will appear.
Input the
target string and click the
OK button. All files with the target string from the sub-folders will open automatically.
The following video shows an overview of the process.
I hope this resolves your problem.
Don’t forget to change the folder path of your required file. The workbook used for this reply is attached below.
Let us know your feedback.
Regards,
Seemanto Saha