Using VBA to list and sort folders and files in Excel

carcharoth2554

New Member
Joined
Feb 5, 2013
Messages
30
Hi guys,

Excel 2010 in Win 7 64bit

I am adapting some code I found in another thread to first list all files found in a target directory and then want it to do some basic sorting. All I am really stuck on is getting the sub ListFilesInFolder() to list the different sub-directories in separate columns; at the moment it reads them all into the same column. I have tried a few approaches on this but cannot figure where I should be putting my column count increase (Dim t). Please see attached code.

Another problem I have had with this code is how slow it is. I have tried adding Application.ScreenUpdating modifiers which cut the processing time in half but for 300+ files I am still looking at 41 seconds. Is there anything that can be done to speed this up or is it just a very large amount of data it is having to handle?

I appreciate any input as you guys have been great in the past.

Thanks!

Code:
Sub GetDirectory()
    Application.Calculation = xlCalculationManual
    Dim CheckPath As String
    Dim Msg As Byte
    Dim Drilldown As Boolean
    CheckPath = "O:\Timesheets and Forms\2013 Timesheets\Test Folder"
    Worksheets("Register").Range("A1:H999").ClearContents
    If CheckPath = "" Then
        MsgBox "No folder was selected.  Procedure aborted.", vbExclamation, "StaffSmart Add-In"
        Exit Sub
    End If
    Msg = MsgBox("Do you want to list all files in subfolders, too?", _
        vbInformation + vbYesNo, "Drill-Down")
    If Msg = vbYes Then Drilldown = True Else Drilldown = False
    ' add headers
    Application.ScreenUpdating = False
    With Range("A1")
        .Value = CheckPath
        .Font.Bold = True
        .Font.Size = 12
    End With
    Range("A3").Formula = "Folder:"
    Range("A3:H3").Font.Bold = True
    ListFilesInFolder CheckPath, Drilldown
    ' list all files included subfolders
    Range("a4").Select
    ActiveWindow.FreezePanes = True
    Range("a3").Sort Key1:=Range("A4"), Order1:=xlAscending, Header:=xlYes
    Range("a3").Select
    ActiveWindow.LargeScroll Up:=100
    Application.ScreenUpdating = True
    MsgBox "Done", vbOKOnly, "StaffSmart Add-In"

End Sub
Private Sub ListFilesInFolder(SourceFolderName As String, IncludeSubfolders As Boolean)
' lists information about the files in SourceFolder
' example: ListFilesInFolder "C:\FolderName\", True
    Dim FSO As Object 'Scripting.FileSystemObject
    Dim SourceFolder As Object 'Scripting.Folder
    Dim SubFolder As Object 'Scripting.Folder
    Dim FileItem As Object 'Scripting.File
    Dim r As Integer
    Dim t As Integer
    Dim c As Integer
    Dim strAddress As String
    Dim strDisplayText As String
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set SourceFolder = FSO.GetFolder(SourceFolderName)
    r = Range("A65536").End(xlUp).Row + 1
    t = 1

' If "descendant" folders also get their files listed, then sub calls itself recursively
    If IncludeSubfolders Then
        For Each SubFolder In SourceFolder.SubFolders
            ListFilesInFolder SubFolder.Path, True
            t = t + 1
        Next SubFolder
    End If

    For Each FileItem In SourceFolder.Files
        ' display file properties
        Cells(r, t).Value = FileItem.Name
        Worksheets("Register").Hyperlinks.Add Anchor:=Cells(r, t), Address:=FileItem.ParentFolder.Path & "\" & FileItem.Name
        r = r + 1 ' next row number
    Next FileItem
    Set FileItem = Nothing
    Set SourceFolder = Nothing
    Set FSO = Nothing
End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Strange: with me your code (unchanged other than the path) gives the subdirectories in column 1 and the main directory in column 3. So I do get a split. Or do you want each subdirectory in a separate column as well?
 
Upvote 0
This will list each subfolder in a separate column and add the subfolder name to row 3

Your problem was that the ListFilesinFolder is a recurring function, so the t was reset to 1 every time. I have now made it a module-level variable, so it will keep its value in the recurrence.
I have also changed the scope of the CheckPath variable, so I can use it in ListFilesinFolder to get the subdirectory name.

<font face=Courier New><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN><br><br>    <SPAN style="color:#00007F">Dim</SPAN> t <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> CheckPath <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br><br><SPAN style="color:#00007F">Sub</SPAN> GetDirectory()<br>    Application.Calculation = xlCalculationManual<br>    <SPAN style="color:#00007F">Dim</SPAN> Msg <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> Drilldown <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN><br>    CheckPath = "O:\Timesheets and Forms\2013 Timesheets\Test Folder"<br>    Worksheets("Register").Range("A1:H999").ClearContents<br>    <SPAN style="color:#00007F">If</SPAN> CheckPath = "" <SPAN style="color:#00007F">Then</SPAN><br>        MsgBox "No folder was selected.  Procedure aborted.", vbExclamation, "StaffSmart Add-In"<br>        <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    Msg = MsgBox("Do you want to list all files in subfolders, too?", _<br>        vbInformation + vbYesNo, "Drill-Down")<br>    <SPAN style="color:#00007F">If</SPAN> Msg = vbYes <SPAN style="color:#00007F">Then</SPAN> Drilldown = <SPAN style="color:#00007F">True</SPAN> <SPAN style="color:#00007F">Else</SPAN> Drilldown = <SPAN style="color:#00007F">False</SPAN><br>    <SPAN style="color:#007F00">' add headers</SPAN><br>    Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br>    <SPAN style="color:#00007F">With</SPAN> Range("A1")<br>        .Value = CheckPath<br>        .Font.Bold = <SPAN style="color:#00007F">True</SPAN><br>        .Font.Size = 12<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    <br>    Range("A3:H3").Font.Bold = <SPAN style="color:#00007F">True</SPAN><br>    t = 1<br>    ListFilesInFolder CheckPath, Drilldown<br>    <SPAN style="color:#007F00">' list all files included subfolders</SPAN><br>    Range("a4").Select<br>    ActiveWindow.FreezePanes = <SPAN style="color:#00007F">True</SPAN><br>    Range("a3").Sort Key1:=Range("A4"), Order1:=xlAscending, Header:=xlYes<br>    Range("a3").Select<br>    ActiveWindow.LargeScroll Up:=100<br>    Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br>    MsgBox "Done", vbOKOnly, "StaffSmart Add-In"<br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> ListFilesInFolder(SourceFolderName <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, IncludeSubfolders <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN>)<br><SPAN style="color:#007F00">' lists information about the files in SourceFolder</SPAN><br><SPAN style="color:#007F00">' example: ListFilesInFolder "C:\FolderName\", True</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> FSO <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN> <SPAN style="color:#007F00">'Scripting.FileSystemObject</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> SourceFolder <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN> <SPAN style="color:#007F00">'Scripting.Folder</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> SubFolder <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN> <SPAN style="color:#007F00">'Scripting.Folder</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> FileItem <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN> <SPAN style="color:#007F00">'Scripting.File</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> r <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> c <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> strAddress <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> strDisplayText <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> FSO = CreateObject("Scripting.FileSystemObject")<br>    <SPAN style="color:#00007F">Set</SPAN> SourceFolder = FSO.GetFolder(SourceFolderName)<br>    r = 4   <SPAN style="color:#007F00">'first row for the file list</SPAN><br><br><SPAN style="color:#007F00">' If "descendant" folders also get their files listed, then sub calls itself recursively</SPAN><br>    <SPAN style="color:#00007F">If</SPAN> IncludeSubfolders <SPAN style="color:#00007F">Then</SPAN><br>        <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> SubFolder <SPAN style="color:#00007F">In</SPAN> SourceFolder.SubFolders<br>            ListFilesInFolder SubFolder.Path, <SPAN style="color:#00007F">True</SPAN><br>            <SPAN style="color:#007F00">' put subfolder name in row 3</SPAN><br>            Cells(3, t) = Right(SubFolder.Path, Len(<SPAN style="color:#00007F">Sub</SPAN>Folder.Path) - Len(CheckPath))<br>            t = t + 1<br>        <SPAN style="color:#00007F">Next</SPAN> SubFolder<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><br>    <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> FileItem <SPAN style="color:#00007F">In</SPAN> SourceFolder.Files<br>        <SPAN style="color:#007F00">' display file properties</SPAN><br>        Cells(r, t).Value = FileItem.Name<br>        Worksheets("Register").Hyperlinks.Add Anchor:=Cells(r, t), Address:=FileItem.ParentFolder.Path & "\" & FileItem.Name<br>        r = r + 1 <SPAN style="color:#007F00">' next row number</SPAN><br>    <SPAN style="color:#00007F">Next</SPAN> FileItem<br>    <SPAN style="color:#00007F">Set</SPAN> FileItem = <SPAN style="color:#00007F">Nothing</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> SourceFolder = <SPAN style="color:#00007F">Nothing</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> FSO = <SPAN style="color:#00007F">Nothing</SPAN><br><SPAN style="color:#00007F">End</SPAN> Sub<br><br></FONT>
 
Upvote 0
If you change the line

Rich (BB code):
    Worksheets("Register").Range("A1:H999").ClearContents


to
Rich (BB code):
    Worksheets("Register").Cells.ClearContents
then it doesn't matter how many subdirectories there are, else it wouldn't delete columns after column H

As for speed, because you are writing several times to each cell (to enter the value and to put the hyperlinking in place, it will be relatively slow. Probably more so if your directories are on a shared drive.
 
Upvote 0
Hi sijpie! Sorry for the late reply, I didn't get a notification for some reason.

You were correct in your assessment; I did want the different sub-dirs listed in separate columns (don't overly care to much about the filepaths, just the sorting of the files themselves). Unfortunately you are also correct in saying the directories are on a shared drive so this is making things very slow. Unfortunately that is not my choice to make so I have to deal with it.

I am going to have a crack at integrating this code this afternoon (as mine has changed slightly from the one posted) and I will update you if this solves things!

These files will rarely be moved or deleted for any reason, the update is more for picking up new additions. If there is no way to speed up the code as you mentioned then I am thinking about instead having this first function for an all over wipe and re-read that is used infrequently and a second more frequently used function that leaves existing data and tries to pick up any files subsequently added. Any thoughts on the feasibility of this? I cannot rely on the static data of the files as these are submitted by dozens of people and the file system is unregulated so all I have to go on is the filename itself reading week1, week2, week3 etc.

Thanks a lot for all your time!
 
Upvote 0
Do you need the hyperlinks to the cells? otherwise I can get all the info into one array and write it only once which will improve speed considerably on large datasets.

Also for a weekly update, we could do something like load all the files in the array, load the current stored data into an other array, compare the two and only add the new ones. That means only first time around will it be slow. We could do this keeping the hyperlinks
 
Upvote 0
Also for a weekly update, we could do something like load all the files in the array, load the current stored data into an other array, compare the two and only add the new ones. That means only first time around will it be slow. We could do this keeping the hyperlinks

Unfortunately I do require the links even if that is at the cost of some runtime. I can deal with that however as the second part of your response sounds right on the mark. Sorry to ask but working with arrays is not a part of my vba strengths, any chance you could outline for me a snippet of how you would read the data into an array please? I think (hope) I will be able to take it from there and integrate that into my code also :)

Thanks again!!
 
Upvote 0
it's a bit tricky until you understand it. I am working on a version right now, so should have it with you in a short while. This question about to include subfolders or not, will you ever answer no? makes coding a bit easier.
 
Upvote 0
it's a bit tricky until you understand it. I am working on a version right now, so should have it with you in a short while. This question about to include subfolders or not, will you ever answer no? makes coding a bit easier.

No, I was intending on removing that code anyway but just had not got around to it yet :) Feel free to remove!

Thanks again! I love this board so much!
 
Upvote 0
Try this code on its own (amend the directory path first). This uses arrays and lists the files in columns, but does not add any hyperlinks yet. Stick it in a file point it to a directory (preferaly a shared large directory) and see if it is significantly quicker.

I will continue with the hyperlink stuff.
<font face=Courier New><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN><br><br>    <SPAN style="color:#00007F">Dim</SPAN> t <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> CheckPath <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> aFD <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN>, aHypL <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN><br><br><SPAN style="color:#00007F">Sub</SPAN> GetDirectory()<br>    Application.Calculation = xlCalculationManual<br>    <SPAN style="color:#00007F">Dim</SPAN> Msg <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN>, i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> Drilldown <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> aOld <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> wsReg <SPAN style="color:#00007F">As</SPAN> Worksheet, rOut <SPAN style="color:#00007F">As</SPAN> Range<br>    <br>    CheckPath = "C:\Users\x01025047\Documents\_Videle\Stage 1\Locations" <SPAN style="color:#007F00">'"O:\Timesheets and Forms\2013 Timesheets\Test Folder"</SPAN><br>    <SPAN style="color:#007F00">' Worksheets("Register").Cells.ClearContents</SPAN><br>    <SPAN style="color:#00007F">If</SPAN> CheckPath = "" <SPAN style="color:#00007F">Then</SPAN><br>        MsgBox "No folder was selected.  Procedure aborted.", vbExclamation, "StaffSmart Add-In"<br>        <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> wsReg = Sheets("Register")<br>    <br>    <SPAN style="color:#00007F">With</SPAN> wsReg<br>        <SPAN style="color:#00007F">Set</SPAN> rOut = .Range("A3") <SPAN style="color:#007F00">' header row of table</SPAN><br>        <br>            <SPAN style="color:#007F00">' load entire existing page in array</SPAN><br>        aOld = rOut.CurrentRegion<br>        <br>        <SPAN style="color:#00007F">With</SPAN> .Range("A1")<br>            .Value = CheckPath<br>            .Font.Bold = <SPAN style="color:#00007F">True</SPAN><br>            .Font.Size = 12<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>            <SPAN style="color:#007F00">'initialise column counter for the arrays</SPAN><br>        t = 1<br>            <SPAN style="color:#007F00">' make filelist array and hyperlink array same size but a bit larger as aOld to start of with, _<br>              so only needs to be resized for large number additional files or directories, as this is a pain</SPAN><br>        <SPAN style="color:#00007F">ReDim</SPAN> aFD(1 <SPAN style="color:#00007F">To</SPAN> 500 + <SPAN style="color:#00007F">UBound</SPAN>(aOld, 1), 1 <SPAN style="color:#00007F">To</SPAN> 20 + <SPAN style="color:#00007F">UBound</SPAN>(aOld, 2))<br>        <SPAN style="color:#00007F">ReDim</SPAN> aHypL(1 <SPAN style="color:#00007F">To</SPAN> 500 + <SPAN style="color:#00007F">UBound</SPAN>(aOld, 1), 1 <SPAN style="color:#00007F">To</SPAN> 20 + <SPAN style="color:#00007F">UBound</SPAN>(aOld, 2))<br>        ListFilesInFolder CheckPath<br>            <SPAN style="color:#007F00">' list all files included subfolders</SPAN><br>        .Range("A3").Resize(1, .Range("A3").End(xlToRight).Column).Font.Bold = <SPAN style="color:#00007F">True</SPAN><br>            <SPAN style="color:#007F00">' print array to sheet</SPAN><br>        <SPAN style="color:#00007F">Set</SPAN> rOut = .Range("a3").Resize(UBound(aFD, 1), <SPAN style="color:#00007F">UBound</SPAN>(aFD, 2))<br>        rOut.Value = aFD<br>            <SPAN style="color:#007F00">' set hyperlinks</SPAN><br>        <SPAN style="color:#00007F">For</SPAN> i = 1 <SPAN style="color:#00007F">To</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(aFD, 2)<br>            <SPAN style="color:#007F00">' to be coded</SPAN><br>        <SPAN style="color:#00007F">Next</SPAN> i<br>        .Range("a4").Select<br>        ActiveWindow.FreezePanes = <SPAN style="color:#00007F">True</SPAN><br>        .Range("a3").Sort Key1:=.Range("A4"), Order1:=xlAscending, Header:=xlYes<br>        .Range("a3").Select<br>        ActiveWindow.LargeScroll Up:=100<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br>    Application.Calculation = xlCalculationAutomatic<br><SPAN style="color:#007F00">'    MsgBox "Done", vbOKOnly, "StaffSmart Add-In"</SPAN><br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> ListFilesInFolder(SourceFolderName <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>)<br><SPAN style="color:#007F00">' lists information about the files in SourceFolder</SPAN><br><SPAN style="color:#007F00">' example: ListFilesInFolder "C:\FolderName\", True</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> FSO <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN> <SPAN style="color:#007F00">'Scripting.FileSystemObject</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> SourceFolder <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN> <SPAN style="color:#007F00">'Scripting.Folder</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> SubFolder <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN> <SPAN style="color:#007F00">'Scripting.Folder</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> FileItem <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN> <SPAN style="color:#007F00">'Scripting.File</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> r <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> c <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> strAddress <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> strDisplayText <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <br>    <SPAN style="color:#00007F">Set</SPAN> FSO = CreateObject("Scripting.FileSystemObject")<br>    <SPAN style="color:#00007F">Set</SPAN> SourceFolder = FSO.GetFolder(SourceFolderName)<br>    r = 2   <SPAN style="color:#007F00">'first row for the file list</SPAN><br><br>    <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> SubFolder <SPAN style="color:#00007F">In</SPAN> SourceFolder.SubFolders<br>        ListFilesInFolder SubFolder.Path<br>        t = t + 1<br>        <SPAN style="color:#00007F">If</SPAN> t > UBound(aFD, 2) <SPAN style="color:#00007F">Then</SPAN><br>           <SPAN style="color:#007F00">' redim aFD & HypL</SPAN><br>           <SPAN style="color:#007F00">' this can be done using Redim Preserve</SPAN><br>           <SPAN style="color:#007F00">' Add 20 columns for some reserve</SPAN><br>           <br>           <SPAN style="color:#00007F">ReDim</SPAN> <SPAN style="color:#00007F">Preserve</SPAN> aFD(UBound(aFD, 1), <SPAN style="color:#00007F">UBound</SPAN>(aFD, 2) + 20)<br>           <SPAN style="color:#00007F">ReDim</SPAN> <SPAN style="color:#00007F">Preserve</SPAN> aHypL(<SPAN style="color:#00007F">UBound</SPAN>(aFD, 1), <SPAN style="color:#00007F">UBound</SPAN>(aFD, 2) + 20)<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <SPAN style="color:#00007F">Next</SPAN> <SPAN style="color:#00007F">Sub</SPAN>Folder<br>            <br>            <SPAN style="color:#007F00">' put subfolder name in first row</SPAN><br>    aFD(1, t) = Right(SourceFolder.Path, Len(SourceFolder.Path) - Len(CheckPath))<br>    <SPAN style="color:#00007F">If</SPAN> Len(aFD(1, t)) = 0 <SPAN style="color:#00007F">Then</SPAN> aFD(1, t) = "\"<br><br>    <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> FileItem <SPAN style="color:#00007F">In</SPAN> SourceFolder.Files<br>        <SPAN style="color:#007F00">' display file properties</SPAN><br>        aFD(r, t) = FileItem.Name<br>        aHypL(r, t) = FileItem.ParentFolder.Path & "\" & FileItem.Name<br>        r = r + 1 <SPAN style="color:#007F00">' next row number</SPAN><br>        <SPAN style="color:#00007F">If</SPAN> r > <SPAN style="color:#00007F">UBound</SPAN>(aFD, 1) <SPAN style="color:#00007F">Then</SPAN><br>           <SPAN style="color:#007F00">' redim aFD & aHypL, add 500 rows</SPAN><br>           <SPAN style="color:#007F00">' Redim Preserve cannot be used to change nr of rows</SPAN><br>           <SPAN style="color:#007F00">' so copy to temp arrays and then increase size and</SPAN><br>           <SPAN style="color:#007F00">' copy back</SPAN><br>           <SPAN style="color:#00007F">Dim</SPAN> aTemp1 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN>, aTemp2 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN><br>           <SPAN style="color:#00007F">Dim</SPAN> i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>, j <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br>           <SPAN style="color:#00007F">ReDim</SPAN> aTemp1(1 <SPAN style="color:#00007F">To</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(aFD, 1), 1 <SPAN style="color:#00007F">To</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(aFD, 2))<br>           <SPAN style="color:#00007F">ReDim</SPAN> aTemp2(1 <SPAN style="color:#00007F">To</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(aFD, 1), 1 <SPAN style="color:#00007F">To</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(aFD, 2))<br>           <SPAN style="color:#00007F">For</SPAN> i = 1 <SPAN style="color:#00007F">To</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(aFD, 1)<br>               <SPAN style="color:#00007F">For</SPAN> j = 1 <SPAN style="color:#00007F">To</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(aFD, 2)<br>                   aTemp1(i, j) = aFD(i, j)<br>                   aTemp2(i, j) = aHypL(i, j)<br>               <SPAN style="color:#00007F">Next</SPAN> j<br>           <SPAN style="color:#00007F">Next</SPAN> i<br>           <SPAN style="color:#00007F">ReDim</SPAN> aFD(500 + <SPAN style="color:#00007F">UBound</SPAN>(aFD, 1), <SPAN style="color:#00007F">UBound</SPAN>(aFD, 2))<br>           <SPAN style="color:#00007F">ReDim</SPAN> aHypL(500 + <SPAN style="color:#00007F">UBound</SPAN>(aFD, 1), UBound(aFD, 2))<br>           <SPAN style="color:#00007F">For</SPAN> i = 1 <SPAN style="color:#00007F">To</SPAN> UBound(aFD, 1)<br>               <SPAN style="color:#00007F">For</SPAN> j = 1 <SPAN style="color:#00007F">To</SPAN> UBound(aFD, 2)<br>                   aFD(i, j) = aTemp1(i, j)<br>                   aHypL(i, j) = aTemp2(i, j)<br>               <SPAN style="color:#00007F">Next</SPAN> j<br>           <SPAN style="color:#00007F">Next</SPAN> i<br>           <SPAN style="color:#00007F">ReDim</SPAN> aTemp1(1, 1)<br>           <SPAN style="color:#00007F">ReDim</SPAN> aTemp2(1, 1)<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <SPAN style="color:#00007F">Next</SPAN> FileItem<br>    <SPAN style="color:#00007F">Set</SPAN> FileItem = <SPAN style="color:#00007F">Nothing</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> SourceFolder = <SPAN style="color:#00007F">Nothing</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> FSO = <SPAN style="color:#00007F">Nothing</SPAN><br><SPAN style="color:#00007F">End</SPAN> Sub<br><SPAN style="color:#007F00">''Private Sub ListFilesInFolder(SourceFolderName As String, IncludeSubfolders As Boolean)</SPAN></FONT>
 
Upvote 0

Forum statistics

Threads
1,216,098
Messages
6,128,812
Members
449,468
Latest member
AGreen17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top