+ Reply to Thread
Results 1 to 12 of 12

List files in folders in excel

Hybrid View

jayblack List files in folders in excel 05-18-2010, 09:26 AM
foxguy Re: List files in folders in... 05-18-2010, 06:04 PM
jayblack Re: List files in folders in... 05-19-2010, 07:38 AM
foxguy Re: List files in folders in... 05-19-2010, 09:08 AM
jayblack Re: List files in folders in... 05-19-2010, 09:24 AM
foxguy Re: List files in folders in... 05-19-2010, 09:34 AM
jayblack Re: List files in folders in... 05-19-2010, 09:37 AM
foxguy Re: List files in folders in... 05-19-2010, 01:31 PM
jayblack Re: List files in folders in... 05-19-2010, 02:34 PM
foxguy Re: List files in folders in... 05-19-2010, 04:24 PM
foxguy Re: List files in folders in... 05-20-2010, 12:56 PM
jayblack Re: List files in folders in... 05-21-2010, 07:25 AM
  1. #1
    Registered User
    Join Date
    05-18-2010
    Location
    canada
    MS-Off Ver
    Excel 2007
    Posts
    7

    List files in folders in excel

    Hey,

    iv'e been searching for a few days now and i can't seem to find a code to list files in folders.

    I am using excel 2007, my knowledge of macro's is more or less limited.

    what i need is a program that can find .docx files in a folder and its subfolders. and list them in excel. Date last modified would be nice to have tho.

    i need to be able to save the xl file and update it when it opens if possible.

    any help is appreciated...my boss needs this bad haha

    Regards, Jason

  2. #2
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: List files in folders in excel

    This should do what you want.
    This will put the list in the 1st 3 columns of the ActiveSheet.
    Edit ShowFileList() to put in the folder and extension desired.
    Just select the sheet you want the list on, make sure the 1st 3 columns don't have anything in them you want to keep, and run "ShowFileList()"
    Be sure and back up your workbook 1st, in case there's a bug I didn't see.
    Sub ShowFileList()
        Dim sFolder as string, sExtension as string
    
        'Change the Folder and extension to proper choices
        sFolder = "C:\Users\Bill\Documents\Computer\Excel\Excel Help"
        sExtension = ".xls"
    
        ListFolderFiles sFolder, sExtension
    End Sub
    
    Sub ListFolderFiles(FolderSpec, FileExtension, Optional ByRef vFiles)
        Dim fs, f1, fc, s, fFolder, fDate As Date
        Dim lCount As Long
        Dim fSubFolders, fSubFolder, bSubFolder As Boolean
        Dim aFiles()
        Dim r As Range
    
        Set fs = CreateObject("Scripting.FileSystemObject")
        If Not fs.FolderExists(FolderSpec) Then
            MsgBox FolderSpec & " is not a valid folder"
        Else
            Set fFolder = fs.GetFolder(FolderSpec)
            Set fc = fFolder.Files
            Set fSubFolders = fFolder.subfolders
            FileExtension = Trim(LCase(FileExtension)) & " "
            If Not IsMissing(vFiles) Then
                bSubFolder = True
                aFiles() = vFiles
                lCount = UBound(aFiles(), 2) + 1
            Else
                ReDim aFiles(1 To 3, 1 To 1)
                aFiles(1, 1) = "Folder"
                aFiles(2, 1) = "File"
                aFiles(3, 1) = "Last Modified"
                lCount = 2
            End If
            ReDim Preserve aFiles(1 To 3, 1 To lCount + 1)
            aFiles(1, lCount + 1) = FolderSpec
    
            For Each f1 In fc
                If InStr(LCase(f1) & " ", FileExtension) > 0 Then
                    lCount = lCount + 1
                    ReDim Preserve aFiles(1 To 3, 1 To lCount)
                    aFiles(2, lCount) = f1.Name
                    aFiles(3, lCount) = f1.DateLastModified
                End If
            Next
            If fSubFolders.Count > 0 Then
                For Each fSubFolder In fSubFolders
                    ListFolderFiles fSubFolder, FileExtension, aFiles
                Next fSubFolder
            ElseIf bSubFolder Then
                vFiles = aFiles()
            End If
            If Not bSubFolder Then
                If UBound(aFiles(), 2) > 0 Then
                    With ActiveSheet
                       .Range("A1").Resize(UBound(aFiles(), 2), UBound(aFiles(), 1)).Value = Transpose(aFiles())
    
                        Set r = .Columns("A:C")
                        r.EntireColumn.AutoFit
                    End With
                End If
            End If
        End If
    End Sub
    Foxguy

    Remember to mark your questions [Solved] and rate the answer(s)
    Forum Rules are Here

  3. #3
    Registered User
    Join Date
    05-18-2010
    Location
    canada
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: List files in folders in excel

    hey, thanks for the quick reply! very cool

    I am getting a compile error 35 like i said i'm not very knowledgeable when it comes to excel, i think it has something to do with the name...but thats just a guess

    this is what i see

    \1



    thanks for your help once again!

    Jason

  4. #4
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: List files in folders in excel

    Sorry about that. I saw that you were using Excel 2007 (which I'm not) and I included a function that I thought 2007 had. I was wrong. I should have looked it up first.
    I've put it all in a file, so it should work right.List.xls

  5. #5
    Registered User
    Join Date
    05-18-2010
    Location
    canada
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: List files in folders in excel

    I tried the list file and it gets bogged down,

    i'm not sure if it was for excel 2003 , or did you mean that the new one was for 2007?

    in any case, i tried using the file and it locked on me : S

    Jason

  6. #6
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: List files in folders in excel

    It's written in Excell 2002, but it should still work in 2007.
    I tried moving up 2 folder levels to see if it gets bogged down with too many files, and it didn't list any. There's obviously a bug in it. I'll work on it.

  7. #7
    Registered User
    Join Date
    05-18-2010
    Location
    canada
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: List files in folders in excel

    Alright, thanks fox!

    i appreciate the work, it helps a lot haha

    Jason

  8. #8
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: List files in folders in excel

    jayblack;

    Ok, here is the file List.xls. I tested it on several folders. Also added StatusBar entries, so you can see that it's working and not stuck. It went through over 4900 folders and 120,000 files without a hitch.

  9. #9
    Registered User
    Join Date
    05-18-2010
    Location
    canada
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: List files in folders in excel

    Wow,


    that is very impressive! it works great and is just what i wanted.

    Thanks a bunch fox

    regards, jason

  10. #10
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: List files in folders in excel

    My Pleasure. If you really like it, rate it by clicking the blue scales above right side of screen.

  11. #11
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: List files in folders in excel

    jayblack;

    I'll keep this thread in my subscriptions list for at least a month, in case you write more. After that you'll have to send me a private message to let me know you're back

  12. #12
    Registered User
    Join Date
    05-18-2010
    Location
    canada
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: List files in folders in excel

    alright, thanks

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1