Consulting

Results 1 to 13 of 13

Thread: Copy Folder names into Excel

  1. #1
    VBAX Regular
    Joined
    Aug 2006
    Location
    New York City
    Posts
    23
    Location

    Red face Copy Folder names into Excel

    Hi,

    I have the following that places all file names in a folder into a sheet, however I want the folder names within a folder placed into the sheet. Is this possible?

    [VBA]NextRow = 2
    With Application.FileSearch
    .NewSearch
    .LookIn = "W:\Compliance2\2 - Account Approval Process\2 - CORPORATE CLIENTS"
    .Filename = "*.*"
    .Execute

    FileToProcess = .FoundFiles.Count

    For i = 1 To .FoundFiles.Count
    ThisEntry = .FoundFiles(i)
    Cells(NextRow, 1).Value = ThisEntry

    NextRow = NextRow + 1

    Next i[/VBA]

  2. #2
    VBAX Tutor Erdin? E. Ka's Avatar
    Joined
    Sep 2006
    Location
    Bursa
    Posts
    264
    Location
    Hi bassman71

    Yes, it is possible, try the code below;

    [VBA]
    Sub ListOfFolders()

    Dim i As Integer
    Dim SearchFolders As Variant

    i = 1
    LookInTheFolder = "C:\TheNameOfYourSearchingFolder" ' As you know; you should modificate this row.
    Set FileSystemObject = CreateObject("Scripting.FileSystemObject")

    For Each SearchFolders In FileSystemObject.GetFolder(LookInTheFolder).SubFolders
    Cells(i, 1) = SearchFolders
    i = i + 1
    Next SearchFolders

    End Sub
    [/VBA]
    Erdin? E. Kara?am | Loves from Bursa city in Republic of T?rkiye

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    And you can make this code recursive
    [vba]
    Option Explicit

    Dim i As Long, j As Long
    Dim searchfolders As Variant
    Dim FileSystemObject

    Sub ListOfFolders()
    Dim LookInTheFolder As String

    i = 1
    LookInTheFolder = "C:\" ' As you know; you should modificate this row.
    Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
    For Each searchfolders In FileSystemObject.GetFolder(LookInTheFolder).SubFolders
    Cells(i, 1) = searchfolders
    i = i + 1
    SearchWithin searchfolders
    Next searchfolders

    End Sub

    Sub SearchWithin(searchfolders)
    On Error GoTo exits
    For Each searchfolders In FileSystemObject.GetFolder(searchfolders).SubFolders
    j = UBound(Split(searchfolders, "\"))
    Cells(i, j) = searchfolders
    i = i + 1
    SearchWithin searchfolders
    Next searchfolders
    exits:
    End Sub



    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    VBAX Tutor Erdin? E. Ka's Avatar
    Joined
    Sep 2006
    Location
    Bursa
    Posts
    264
    Location
    Well done mdmackillop,

    I love "Split Function".
    Erdin? E. Kara?am | Loves from Bursa city in Republic of T?rkiye

  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Quote Originally Posted by Erdin? E. Ka
    I love "Split Function".
    Since I came across it, I've found lots of uses for it. It simplifies so many tasks.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    VBAX Tutor Erdin? E. Ka's Avatar
    Joined
    Sep 2006
    Location
    Bursa
    Posts
    264
    Location
    Certainly is it.
    Split is very talented function.
    I have some experriences about it, but i suppose that i should learn more about it.
    Last edited by Erdin? E. Ka; 10-02-2006 at 08:43 PM.
    Erdin? E. Kara?am | Loves from Bursa city in Republic of T?rkiye

  7. #7
    VBAX Regular
    Joined
    Aug 2006
    Location
    New York City
    Posts
    23
    Location
    Really cool! Thanks

  8. #8
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    What if there is a subfolder in the subfolder? Or a subfolder in that? That's why I like the FileSearch method (although it is being shined away from in the upcoming releases of Office, which I am rather displeased with). You could use the Dir method as well though (prefferred).

  9. #9
    VBAX Expert
    Joined
    Jul 2004
    Location
    Wilmington, DE
    Posts
    600
    Location
    If you need some further details on the files, check out my response to:

    http://www.experts-exchange.com/Appl..._22011203.html
    Regards,

    Patrick

    I wept for myself because I had no PivotTable.

    Then I met a man who had no AutoFilter.

    Microsoft MVP for Excel, 2007 & 2008

  10. #10
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Quote Originally Posted by firefytr
    What if there is a subfolder in the subfolder? Or a subfolder in that? That's why I like the FileSearch method (although it is being shined away from in the upcoming releases of Office, which I am rather displeased with). You could use the Dir method as well though (prefferred).
    Dir has an issue that you should be aware of. If you are using Dir to test the existence of a directory, it returns an incorrect result if there are no files in it. In my experience, it could have 10 subfolders with files in them, but if there is no actual file in that directory, you'll still get back False.

    I've documented that in my UDF to check if file/folder exists.

    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  11. #11
    Dear mdmackillop and others,
    your modification for Erdin? E. Ka's script looks very interesting. From what I understand, it is meant to search for subfolders within the folders returned by the first sub (ListOfFolders), isn't it? Unfortunately this does not seem to work for me. If I run ListOfFolders via the macro procedure menu, it will find the folders from the upper folder but not the respective subfolders.

    Maybe this is, because I did not know exaxtly where to put the

    Option Explicit

    Dim i As Long, j As Long
    Dim searchfolders As Variant
    Dim FileSystemObject


    Part. And what do mean with "demo purposes"?

    All help is highly appreciated. :-)

  12. #12
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    The limit is removed (post edited accordingly)
    Paste the whole of the code in Post #3 into a Standard module.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  13. #13
    It works! Which is really strange; the same code in another module works, just as you proposed (not that I had doubted that). In the former module I must have copied the declaration to the wrong position.

    Thanks a lot! :-)

Posting Permissions

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