+ Reply to Thread
Results 1 to 6 of 6

How to use a flexible folder address rather than the current one?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-16-2014
    Location
    London
    MS-Off Ver
    Excel 2003 Excel 2010
    Posts
    189

    How to use a flexible folder address rather than the current one?

    Hi, I found a macro below which can list ALL the file names in the same folder as the saved macro file. My question is: how to adapt the code to allow a more flexible folder as specified, for example, C:\Users.

    Sub ListFilesAndSubfolders()
    
      Dim FSO As Object
      Dim rsFSO As Object
      Dim baseFolder As Object
      Dim file As Object
      Dim folder As Object
      Dim row As Integer
      Dim name As String
    
      'Get the current folder
      Set FSO = CreateObject("scripting.filesystemobject")
      Set baseFolder = FSO.GetFolder(ThisWorkbook.Path)
      Set FSO = Nothing
    
      'Get the row at which to insert
      row = Range("A65536").End(xlUp).row + 1
    
      'Create the recordset for sorting
      Set rsFSO = CreateObject("ADODB.Recordset")
      With rsFSO.Fields
        .Append "Name", 200, 200
        .Append "Type", 200, 200
      End With
      rsFSO.Open
    
      ' Traverse the entire folder tree
      TraverseFolderTree baseFolder, baseFolder, rsFSO
      Set baseFolder = Nothing
    
      'Sort by type and name
      rsFSO.Sort = "Type ASC, Name ASC "
      rsFSO.MoveFirst
    
      'Populate the first column of the sheet
      While Not rsFSO.EOF
        name = rsFSO("Name").Value
        If (name <> ThisWorkbook.name) Then
          Cells(row, 1).Formula = name
          row = row + 1
        End If
        rsFSO.MoveNext
      Wend
    
      'Close the recordset
      rsFSO.Close
      Set rsFSO = Nothing
    
    End Sub
    
    Private Sub TraverseFolderTree(ByVal parent As Object, ByVal node As Object, ByRef rs As Object)
    
      'List all files
      For Each file In node.Files
    
        Dim name As String
        name = Mid(file.Path, Len(parent.Path) + 2)
    
        rs.AddNew
        rs("Name") = name
        rs("Type") = "FILE"
        rs.Update
      Next
    
      'List all folders
      For Each folder In node.SubFolders
        TraverseFolderTree parent, folder, rs
      Next
    
    End Sub

  2. #2
    Forum Contributor
    Join Date
    01-16-2014
    Location
    London
    MS-Off Ver
    Excel 2003 Excel 2010
    Posts
    189

    Re: How to use a flexible folder address rather than the current one?

    I'm still looking for a help

    The location is:

    Set baseFolder = FSO.GetFolder(ThisWorkbook.Path)

    How can I change this to folder: C:\Users

  3. #3
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: How to use a flexible folder address rather than the current one?

    hi alice2011, try this option (new lines are underlined):

    Sub ListFilesAndSubfolders()
    
      Dim FSO As Object
      Dim rsFSO As Object
      Dim baseFolder As Object
      Dim file As Object
      Dim folder As Object
      Dim row As Integer
      Dim name As String
      Dim fd As Object, folder2process As String
      
      'Choose folder
      Set fd = Application.FileDialog(msoFileDialogFolderPicker)
      If fd.Show = -1 Then folder2process = fd.SelectedItems(1) Else Exit Sub
      
      'Get the current folder
      Set FSO = CreateObject("scripting.filesystemobject")
      Set baseFolder = FSO.GetFolder(folder2process)
      Set FSO = Nothing
    
      'Get the row at which to insert
      row = Range("A65536").End(xlUp).row + 1
    
      'Create the recordset for sorting
      Set rsFSO = CreateObject("ADODB.Recordset")
      With rsFSO.Fields
        .Append "Name", 200, 200
        .Append "Type", 200, 200
      End With
      rsFSO.Open
    
      ' Traverse the entire folder tree
      TraverseFolderTree baseFolder, baseFolder, rsFSO
      Set baseFolder = Nothing
    
      'Sort by type and name
      rsFSO.Sort = "Type ASC, Name ASC "
      rsFSO.MoveFirst
    
      'Populate the first column of the sheet
      While Not rsFSO.EOF
        name = rsFSO("Name").Value
        If (name <> ThisWorkbook.name) Then
          Cells(row, 1).Formula = name
          row = row + 1
        End If
        rsFSO.MoveNext
      Wend
    
      'Close the recordset
      rsFSO.Close
      Set rsFSO = Nothing
    
    End Sub
    
    Private Sub TraverseFolderTree(ByVal parent As Object, ByVal node As Object, ByRef rs As Object)
    
      'List all files
      For Each file In node.Files
    
        Dim name As String
        name = Mid(file.Path, Len(parent.Path) + 2)
    
        rs.AddNew
        rs("Name") = name
        rs("Type") = "FILE"
        rs.Update
      Next
    
      'List all folders
      For Each folder In node.SubFolders
        TraverseFolderTree parent, folder, rs
      Next
    
    End Sub

  4. #4
    Forum Contributor
    Join Date
    01-16-2014
    Location
    London
    MS-Off Ver
    Excel 2003 Excel 2010
    Posts
    189

    Re: How to use a flexible folder address rather than the current one?

    Hi watersev, I totally feel amazed by this great improvement of the code. The new one is very flexible to allow me to choose the folder. Really good . Hope others can use it too.

    Thanks for the great help.

  5. #5
    Forum Contributor
    Join Date
    01-16-2014
    Location
    London
    MS-Off Ver
    Excel 2003 Excel 2010
    Posts
    189

    Re: How to use a flexible folder address rather than the current one?

    Sorry to come back. I ran the code and it works ok. But sometimes it failed to work and issued an error message as:

    Run-time error '-2147217887 (80040e21)'):
    Multiple-step operation generated errors. Check each status value.

    The code may have some drawback behind which I don't know (I found it online).

    Anyone knows?

  6. #6
    Forum Contributor
    Join Date
    01-16-2014
    Location
    London
    MS-Off Ver
    Excel 2003 Excel 2010
    Posts
    189

    Re: How to use a flexible folder address rather than the current one?


+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Entering current folder into a cell
    By Nimril in forum Excel General
    Replies: 3
    Last Post: 10-30-2013, 07:25 PM
  2. [SOLVED] Save worksheets in current folder as .pdf
    By Birdster in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-11-2013, 04:44 AM
  3. Get current cell address
    By The_Vulcan in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-20-2006, 10:39 PM
  4. Dynamic flexible cell address
    By dewsbury in forum Excel General
    Replies: 2
    Last Post: 03-09-2006, 04:10 PM
  5. HELP! saving as xls in current folder
    By drumerboy in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-18-2006, 10:10 AM

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