Hi,
I'm trying to make a macro that searches through a master folder and its subfolders for a files that start with the Code (eg: Code123) where the 123 are what users would input through the inputbox function. Using the code provided by jbeaucaire to loop through subfolders some time ago, I tried to incorporate inputbox into it but the macro would simply run and do nothing. How do I make it loop through each subfolder to look for a certain code number until the code is found and opened? The code i have is as below:
Sub MacTest()
Dim fNAME As String
Dim fPATH As String: fPATH = "E:\Desktop\CR\"
Dim Fso As Object: Set Fso = CreateObject("Scripting.FileSystemObject")
Dim FLD As Object: Set FLD = Fso.GetFolder(fPATH)
Dim SubFLDRS As Object: Set SubFLDRS = FLD.subfolders
Dim SubFLD As Object
Dim wbData As Workbook
Dim File As Variant
File = InputBox("Enter code:", "What is the code?", "")
Application.ScreenUpdating = False
For Each SubFLD In SubFLDRS
fNAME = Dir(fPATH & SubFLD.Name & "\Code" & File & ".xlsx")
Do While Len(fNAME) > 0
Set wbData = Workbooks.Open(fPATH & SubFLD.Name & "\" & fNAME)
wbData.Close False
fNAME = Dir
Loop
Next SubFLD
Application.ScreenUpdating = True
End Sub
I tried using a more straight forward approach but the code didnt work.
Sub cashrectest3()
On Error GoTo ErrorTrap
Dim myDpc As String
Dim myPath As String
Dim myFile As Variant
Dim SubFLD As Object
Dim SubFLDRS As Object: Set SubFLDRS = FLD.subfolders
myFile = InputBox _
("Enter the fund code:", _
"Which fund?", _
"")
If myFile = "" Then Exit Sub Else
myPath = "E:\Desktop\CR\"
myDoc = myPath & SubFLD.Name & "\" & "Code" & myFile & ".xlsx "
Workbooks.Open Filename:=myDoc
Exit Sub
ErrorTrap:
MsgBox ("File not found!")
Exit Sub
End Sub
This is my first try at VB. Advices are greatly appreciated.
Thanks a lot.
Sam.
Bookmarks