Hi Everyone,
I'm looking for some VBA help. The below code essentially opens each file in the main folder and checks for data. It then returns any values it is supposed to, then adds it to the next row to make a list. This part works great. Now the problem is I am changing the file location. The files used to all be in one main folder, the main folder will now only have sub-folders, and the files will be inside the sub-folders. I know that I need to loop through the subfolders, but I have tried some loop coding, but I can't get it to function.
Thanks
I'm looking for some VBA help. The below code essentially opens each file in the main folder and checks for data. It then returns any values it is supposed to, then adds it to the next row to make a list. This part works great. Now the problem is I am changing the file location. The files used to all be in one main folder, the main folder will now only have sub-folders, and the files will be inside the sub-folders. I know that I need to loop through the subfolders, but I have tried some loop coding, but I can't get it to function.
Thanks
Code:
Sub OVERDUEcheck()
Dim sPath As String, sName As String
Dim bk As Workbook 'opened from the folder
Dim src As Worksheet 'sheet to retrieve data from
Dim sh As Worksheet 'the sheet with the command button
Dim rw As Long 'the row to write to on sh
Dim lr As Long 'last row col A of src sheet
Dim i As Integer 'for looping rows to look at
Set sh = ActiveSheet ' I will record the value and workbook name
' in the activesheet when the macro runs
rw = 2 ' which row to write to in the activesheet
sPath = "C:\MAINFOLDER_PATH" ' Path for file location
sName = Dir(sPath & "*.xls")
Do While sName <> "" 'Loop until filename is blank
Set bk = Workbooks.Open(sPath & sName)
Set src = bk.Worksheets(2)
With src
If .Range("B7").Text = "Y" Then
lr = .Range("A" & Rows.Count).End(xlUp).Row
For i = 16 To lr
If .Cells(i, "B").Text = "OVERDUE" Then
sh.Cells(rw, "A") = .Range("b5")
sh.Cells(rw, "B") = .Range("b6")
sh.Cells(rw, "C") = .Range("b10")
sh.Cells(rw, "D") = .Range("b11")
sh.Cells(rw, "E") = .Range("a" & i)
sh.Cells(rw, "F") = .Range("B12")
rw = rw + 1
End If
Next i
End If
End With
bk.Close SaveChanges:=False
sName = Dir()
Loop ' loop until no more files
End Sub