jakestyeen7059
New Member
- Joined
- Nov 27, 2013
- Messages
- 7
Hello,
I have been searching for a solution to a problem for quite some times now, and I was hoping one of you have a great idea in this forum.
I first like to appreciate any help and any reply to my problem.
My problem is, I have an excel macro that does multiple thing for a report purpose. Everything worked fine but I had to update the macro to prepare for the new year. My code will go through each Dir and find a specific file named "Master.xlsx" and save this as other file name with report in this file. Master file is just a template to be used.
The problem is Master file can be located anywhere in the subfolders under the main directory. Without further ado, here is the partial code I have. After many weeks of searching, this seemed the right code. It came from --> http://www.mrexcel.com/forum/excel-...r-visual-basic-applications-excel-2010-a.html
All declaration is as followings
The problem with this is, under strDefpath & strBBC, there could be many more nested subfolders. I thought the above code will be able to recursively search through each and every one of the folder but as I keep adding debug.print (originally, it was workbook.open command there but just for test purpose, it is replaced with debug), I found out it will search the subfolder, but does not go into the subfolder. It just reads subfolder as another object.
Please note that IsWorkbookOpen is function taken from somewhere long time ago and that works. Also please note that there could be more than one, max of 2 Master.xlsx thus I needed to open it by comparing the modified time/date.
Any help is appreciated!
Yours truly,
Jake
I have been searching for a solution to a problem for quite some times now, and I was hoping one of you have a great idea in this forum.
I first like to appreciate any help and any reply to my problem.
My problem is, I have an excel macro that does multiple thing for a report purpose. Everything worked fine but I had to update the macro to prepare for the new year. My code will go through each Dir and find a specific file named "Master.xlsx" and save this as other file name with report in this file. Master file is just a template to be used.
The problem is Master file can be located anywhere in the subfolders under the main directory. Without further ado, here is the partial code I have. After many weeks of searching, this seemed the right code. It came from --> http://www.mrexcel.com/forum/excel-...r-visual-basic-applications-excel-2010-a.html
Code:
If Len(Dir(strDefpath & strBBC & strDirYear, vbDirectory)) = 0 Then
MkDir (strDefpath & strBBC & strDirYear)
End If
If Len(Dir(strDefpath & strBBC & strDirYear & strDirType, vbDirectory)) = 0 Then
MkDir (strDefpath & strBBC & strDirYear & strDirType)
End If
If Dir(strDefpath & strBBC & strDirYear & strDirType & "\" & strFilename & ".xlsx") = "" Then
For Each fdr(1) In CreateObject("Scripting.FileSystemObject").GetFolder(strDefpath).SubFolders
On Error Resume Next
For Each target(1) In fdr(1).Files
If InStr(1, UCase(target(1)), UCase(".xlsx")) > 0 Then
If target(1).DateLastModified > dteFile(1) Then
If UCase(target(1).Name) = "MASTER.XLSX" Then
dteFile(1) = target(1).DateLastModified
strFile(1) = target(1)
Debug.Print strFile(1)
End If
End If
End If
Next target(1)
Next fdr(1)
For Each fdr(2) In CreateObject("Scripting.FileSystemObject").GetFolder(strDefpath & strBBC).SubFolders
On Error Resume Next
For Each target(2) In fdr(2).Files
If InStr(1, UCase(target(2)), UCase(".xlsx")) > 0 Then
If target(2).DateLastModified > dteFile(2) Then
If UCase(target(2).Name) = "MASTER.XLSX" Then
dteFile(2) = target(2).DateLastModified
strFile(2) = target(2)
End If
End If
End If
Next target(2)
Next fdr(2)
If dteFile(1) > dteFile(2) Then
If Len(Dir(strFile(1))) Then
Debug.Print strFile(1)
End If
ElseIf dteFile(2) > dteFile(1) Then
If Len(Dir(strFile(2))) Then
Debug.Print strFile(2)
End If
ElseIf dteFile(1) = dteFile(2) Then
If Len(Dir(strFile(1))) Then
Debug.Print strFile(1)
ElseIf Len(Dir(strFile(2))) Then
Debug.Print strFile(2)
End If
End If
ActiveWorkbook.SaveAs strDefpath & strBBC & strDirYear & strDirType & "\" & strFilename & ".xlsx", FileFormat:=51
End If
If Not IsWorkBookOpen(strDefpath & strBBC & strDirYear & strDirType & "\" & strFilename & ".xlsx") Then
Workbooks.Open (strDefpath & strBBC & strDirYear & strDirType & "\" & strFilename & ".xlsx")
Else
Workbooks(strFilename & ".xlsx").Activate
End If
All declaration is as followings
Code:
Dim strFilename As String, strDefpath As String, strDirYear As String, strDirType As String, strPathname As String, strBBC As String
Dim wbMaster As Workbook
Dim fdr(2) As Folder, target(2) As File, dteFile(2) As Date, strFile(2) As String
The problem with this is, under strDefpath & strBBC, there could be many more nested subfolders. I thought the above code will be able to recursively search through each and every one of the folder but as I keep adding debug.print (originally, it was workbook.open command there but just for test purpose, it is replaced with debug), I found out it will search the subfolder, but does not go into the subfolder. It just reads subfolder as another object.
Please note that IsWorkbookOpen is function taken from somewhere long time ago and that works. Also please note that there could be more than one, max of 2 Master.xlsx thus I needed to open it by comparing the modified time/date.
Any help is appreciated!
Yours truly,
Jake