Hello All
I am new to VB script and would like help in processing data 2 to 3 level deep.
I currently am running a script that will read specific lines from an excel files that is stored within main folder one level deep. How do i modify the existing code to read excel data from files stored 2 or sometimes 3 level deep in sub folders . can you help me with modifying this script.
Sub Consolidated_Trail1()
'Processes all subfolders in a main folder, one level deep
Dim FSO As Object, FLD As Object, SubFLDRS As Object, SubFLD As Object, f As Object
Dim fNAME As String, fPATH As String, NextRw As Long
Dim wsMain As Worksheet, wbData, WsSrc, WckSrc As Workbook
'Set f = FSO.GetFolder("C:\Documents and Settings\xxxx\Desktop\xxx")
fPATH = "N:\PxxxxxI\" 'don't forget the final \ in this string
Set FSO = CreateObject("Scripting.FileSystemObject")
Set FLD = FSO.GetFolder(fPATH)
Set SubFLDRS = FLD.SubFolders
Set wsMain = ThisWorkbook.ActiveSheet
Application.ScreenUpdating = False
wsMain.UsedRange.Offset(1).EntireRow.ClearContents
NextRw = 2
wsMain.Range("A1:P1").Value = Array("Project Desc.", "Component", "Project No.", "MPI No.", "99", "A", "L", "B", "C", "D", "F", "G", "Total", "Status", "Filename", "Dwg No.")
For Each SubFLD In SubFLDRS
fNAME = Dir(fPATH & SubFLD.Name & "\" & "*.xls")
Do While Len(fNAME) > 0
Set wbData = Workbooks.Open(fPATH & SubFLD.Name & "\" & fNAME)
Set WsSrc = wbData.ActiveSheet
wsMain.Range("A" & NextRw).Value = WsSrc.Range("A11")
wsMain.Range("B" & NextRw).Value = WsSrc.Range("F11")
wsMain.Range("C" & NextRw).Value = WsSrc.Range("L11")
wsMain.Range("D" & NextRw).Value = WsSrc.Range("J11")
wsMain.Range("E" & NextRw).Value = WsSrc.Range("Q14")
wsMain.Range("F" & NextRw).Value = WsSrc.Range("Q15")
wsMain.Range("G" & NextRw).Value = WsSrc.Range("Q20")
wsMain.Range("H" & NextRw).Value = WsSrc.Range("Q16")
wsMain.Range("I" & NextRw).Value = WsSrc.Range("Q17")
wsMain.Range("J" & NextRw).Value = WsSrc.Range("Q21")
wsMain.Range("K" & NextRw).Value = WsSrc.Range("Q19")
wsMain.Range("L" & NextRw).Value = WsSrc.Range("Q18")
wsMain.Range("M" & NextRw).Value = WsSrc.Range("Q22")
wsMain.Range("N" & NextRw).Value = WsSrc.Range("R14")
wsMain.Range("O" & NextRw).Value = fNAME
wsMain.Range("P" & NextRw).Value = WsSrc.Range("A7")
wbData.Close False
NextRw = NextRw + 1
fNAME = Dir
Loop
Next SubFLD
End Sub
Private Sub CommandButton1_Click()
Consolidated_Trail1
End Sub
Moderator Note:
Welcome to the forum. Pls be sure that you use code tags around your codes when you post.
.
Bookmarks