PDA

View Full Version : Loop through subfolders



jono19872006
08-15-2012, 07:19 AM
I have simplified my code down a lot but below is some code i have which loops through a folder and then formats the documents inside.

However what i use it for is to edit reports and i want to at times do more than one months worth of reports (these by default have the same name)

As such is there a way where i could either specify two files paths which the code would look in and loop through or how could i adjust the below so that It looks at the foder then loops through down into the subfolders?




Dim fPath As String
Dim fName As String
Dim wb As Workbook

Application.ScreenUpdating = False

fPath = "ad\store\Personal\M\maginj90\MACROTEST\"
fName = Dir(fPath & "*.xls") 'start a list of filenames
Do While Len(fName) > 0
Set wb = Workbooks.Open(fPath & fName) 'open found file

'Formats
Range("A1").Value = Now()
Range("A2").Value = "34535s"

'looping round
Application.DisplayAlerts = False
wb.Close True 'close/save
Application.DisplayAlerts = True
fName = Dir 'get next filename
Loop
Application.ScreenUpdating = True
End Sub

thanks
jon

Bob Phillips
08-15-2012, 07:36 AM
Where is the month id in that folder name?

jono19872006
08-15-2012, 07:44 AM
That is just an example of my current code - but my idea is to use the path as per above as a main folder, then have two subfolders inside ie August 2012, September 2012

Bob Phillips
08-15-2012, 07:53 AM
You would have the looping code in a separate procedure with the path as its argument/parameter, and call it twice passing the appropriate path each time.