userform treeview to view files within folders and subfolders

dshafique

Board Regular
Joined
Jun 19, 2017
Messages
171
Hi everyone, I have been banginf my head on this for quite a while and can't figure it out. I want a userform which lets me select a folder, and then once i select it, it shows me all the files not only within that folder, but all the subfolders as well.
i have it so it shows me all the files within the folder, but not within the sub folders.

master folder
--------------folder 1
----------------------file 1
----------------------file 2
----------------------file 2
--------------folder 2
----------------------file 1
--------------folder 3
----------------------file 1
----------------------file 2

how can I get it to loop through all the folders within my selected folder?

any help would be appreciated.
thanks
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
.

Place the path in A1 ( ex: C:\Users\My\Desktop )
Code:
Option Explicit


Public Position As Integer
Public Indent As Integer


Sub ListFileTree()


Position = 0
Indent = 0


Call RecurseFolderList(Range("A1").Value)


End Sub


Private Sub ClearFormatting(Rng As Range)


    Rng.Formula = Rng.Value2
    Rng.Font.ColorIndex = xlAutomatic
    Rng.Font.Underline = xlUnderlineStyleNone


End Sub


Function GetFilenameFromPath(ByVal strPath As String) As String
    If Right$(strPath, 1) <> "\" And Len(strPath) > 0 Then
        GetFilenameFromPath = GetFilenameFromPath(Left$(strPath, Len(strPath) - 1)) + Right$(strPath, 1)
    End If
End Function


Function RecurseFolderList(FolderName As String) As Boolean
    On Error Resume Next
    Dim FSO, NextFolder, FolderArray, FileArray, NextFile
    Dim OriginalRange As Range
    Dim RemoveHyperlink As Boolean
    Set FSO = CreateObject("Scripting.FileSystemObject")


    If Err.Number > 0 Then
        RecurseFolderList = False
    Exit Function


    End If


    On Error GoTo 0
    If FSO.FolderExists(FolderName) Then


        Set NextFolder = FSO.GetFolder(FolderName)
        Set FolderArray = NextFolder.SubFolders
        Set FileArray = NextFolder.Files


        RemoveHyperlink = False
        Set OriginalRange = Range("A2").Offset(Position - 1, Indent)


        Indent = Indent + 1


        For Each NextFolder In FolderArray


            Range("A2").Offset(Position, Indent).Formula = "=HYPERLINK(""" & NextFile & """,""" & UCase(GetFilenameFromPath(NextFolder)) & """)"
            Position = Position + 1


            RecurseFolderList (NextFolder)


            RemoveHyperlink = True
        Next


        For Each NextFile In FileArray


            Range("A2").Offset(Position, Indent).Formula = "=HYPERLINK(""" & NextFile & """,""" & GetFilenameFromPath(NextFile) & """)"
            Position = Position + 1


            RemoveHyperlink = False


            DoEvents
        Next


        If RemoveHyperlink Then
            Call ClearFormatting(OriginalRange)
        End If


        Set NextFolder = Nothing
        Set FolderArray = Nothing
        Set FileArray = Nothing
        Set NextFile = Nothing


    Else
        RecurseFolderList = False
    End If


    Set FSO = Nothing
    Indent = Indent - 1


End Function
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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