hi,
I haven't gone through your code but I have working code that loops through sub-folders. I use occasionally & it works well for me so hopefully you can modify it to incorporate your formatting code (instead of it creating a list of files on a new sheet). Here it is - unmodified, so you'll need to modify the "guts" of the GetFiles macro...
Option Explicit
'http://excel.tips.net/Pages/T002285_Displaying_the_Last_Modified_Date.html
'This module of macros uses the technique of recursion to examine the contents of a folder structure no matter how complex it is.
'sourced (& then modified) from http://homepage.ntlworld.com/martin.rice1/book1.xls on the following site _
http://homepage.ntlworld.com/martin.rice1/example_of_automated_spreadsheet.htm
'other info has been used sourced from _
http://www.erlandsendata.no/english/index.php?d=envbafolderslistfoldersscripting _
http://www.erlandsendata.no/english/index.php?d=envbafolderslistfilesscripting
'Sub GetFiles() ' sourced from http://excelforum.com/showthread.php?t=492643&highlight=listing+file+names
'GetFolder is sourced from p 370 & ListFiles sourced from p769 of Excel 2002 Power Programming with VBA
'*** If the macros in this module do not work, please read the following line as the instruction may allow the macros to work.
'Microsoft Scripting Runtime is included in these products: Windows98, Windows2000, IE5, and Office2000. _
The macro examples below assumes that your VBA project has added a reference to the Microsoft Scripting Runtime library. _
You can do this from within the VBE by selecting the menu Tools, References and selecting Microsoft Scripting Runtime.
Public FirstTimeListingSubFolders As Boolean
Public ListSubFolders As VbMsgBoxResult
Public ListAllDetails As VbMsgBoxResult
Public fs As Object, f As Object
Sub ListFilesAndFolders()
Dim RequestedDirectory As String
Dim N As Long
Dim ConvertToHyperlinks As VbMsgBoxResult
Application.ScreenUpdating = False
'insert & name a new sheet
Sheets.Add
ActiveSheet.Name = "List of Files"
'identify if all details are required to be listed - this will be slower.
ListAllDetails = MsgBox("Press [yes] to list all details or [no] for just the file & folder names", vbYesNo)
Cells(5, 1) = "Folder"
Cells(5, 2) = "File"
Select Case ListAllDetails
Case Is <> vbYes
'leave blank
Case Is = vbYes
Cells(5, 3) = "Size"
Cells(5, 4) = "Type"
Cells(5, 5) = "Date Created"
Cells(5, 6) = "Date Last Accessed"
Cells(5, 7) = "Date Last Modified"
Cells(5, 8) = "Attributes"
End Select
'GetFolder is sourced from p 370 & ListFiles sourced from p769 of Excel 2002 Power Programming with VBA
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = CurDir
.title = "Please choose folder to list files from"
.Show
If .SelectedItems.Count = 0 Then MsgBox "no folder selected": Exit Sub
RequestedDirectory = .SelectedItems(1)
End With
'gather user input at start of macro
ListSubFolders = MsgBox("Do you want to list the files in the sub folders?", vbYesNo, "LIST THE FILES IN THE SUB FOLDERS?")
ConvertToHyperlinks = MsgBox("do you want to convert the files listed to hyperlinks?", vbYesNo)
''The GetSubDirectories subroutine is called recursively using the name of the parent folder.
Call GetSubDirectories(RequestedDirectory, ListSubFolders)
''Hyperlinks (formulae not just cell formatting using [ctrl + k] due to occassional issues) are added to all entries in columns 1 & 2
''other background info 3/4's down the following sheet http://www.mvps.org/dmcritchie/excel/sheets.htm
''Q: can this be done in one hit w/o looping?
''A: appears not!
Select Case ConvertToHyperlinks
Case Is <> vbYes
'no action
Case Is = vbYes
For N = 6 To Cells(Rows.Count, 1).End(xlUp).Row
With Cells(N, 1)
.Value = "=hyperlink(" & Chr(34) & .Value & Chr(34) & ", " & Chr(34) & .Value & Chr(34) & ")"
With .Offset(0, 1)
.Value = "=hyperlink(" & Chr(34) & .Value & Chr(34) & ", " & Chr(34) & .Value & Chr(34) & ")"
End With
End With
Next N
End Select
Columns.AutoFit
Application.ScreenUpdating = True
Set fs = Nothing
Set f = Nothing
MsgBox "done"
End Sub
Sub GetSubDirectories(folderspec, ListSubFolders As VbMsgBoxResult)
Application.ScreenUpdating = False
Dim SubFolder 'not sure what this sould be?
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(folderspec)
Call GetFiles(f.path, ListAllDetails)
Select Case ListSubFolders
Case Is = vbYes 'value = 6
For Each SubFolder In f.SubFolders
Call GetSubDirectories(SubFolder.path, ListSubFolders) 'This is a recursive call
Next SubFolder
Case Else 'case is = 7 'vbno or False or Case Is = 2 'vbcancel or Cancelled
End Select
End Sub
Sub GetFiles(folderspec, ListAllDetails As VbMsgBoxResult)
Dim r As Long
Dim file 'not sure what this should be?
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(folderspec)
For Each file In f.Files
On Error Resume Next
r = Range("A" & Rows.Count).End(xlUp).Row + 1
Cells(r, 1) = folderspec
Cells(r, 2) = folderspec & "\" & file.Name
Select Case ListAllDetails
Case Is <> vbYes
'leave blank
Case Is = vbYes
Cells(r, 3) = file.Size
Cells(r, 4) = file.Type
Cells(r, 5) = file.DateCreated
Cells(r, 6) = file.DateLastAccessed
Cells(r, 7) = file.DateLastModified
Cells(r, 8) = file.Attributes
End Select
On Error GoTo 0
Application.ScreenUpdating = False
Next file
Application.ScreenUpdating = False
End Sub
hth
Rob
Bookmarks