Maybe this might be useful?
Option Explicit
Private objFSO As Object ' Declare FileSystemObject globally within the UserForm for efficiency
Private Sub UserForm_Initialize()
' Set a default folder path when the UserForm initializes
' This will default to the user's Desktop folder.
Me.txtFolderPath.Text = Environ("USERPROFILE") & "\Desktop"
Me.lblFileCount.Caption = "Files Found: 0" ' Reset count
Me.lstFiles.Clear ' Clear any previous list
End Sub
Private Sub cmdListFiles_Click()
Dim colFiles As New Collection ' Collection to store file paths
Dim strFolderPath As String
Dim objRootFolder As Object
Dim varFile As Variant
Me.lstFiles.Clear ' Clear the listbox before populating
Me.lblFileCount.Caption = "Searching..." ' Provide feedback to the user
strFolderPath = Trim(Me.txtFolderPath.Text)
' Initialize FileSystemObject if it hasn't been already
If objFSO Is Nothing Then
Set objFSO = CreateObject("Scripting.FileSystemObject")
End If
' Validate the folder path
If objFSO.FolderExists(strFolderPath) Then
On Error GoTo ErrorHandler ' Enable error handling for folder access
Set objRootFolder = objFSO.GetFolder(strFolderPath)
' Call the recursive function to populate the collection with file paths
Call GetAllFiles(objRootFolder, colFiles)
' Populate the ListBox with the found file paths
For Each varFile In colFiles
Me.lstFiles.AddItem varFile
Next varFile
' Update the file count label
Me.lblFileCount.Caption = "Files Found: " & colFiles.Count & " files."
Else
MsgBox "The specified folder path does not exist. Please enter a valid path.", vbExclamation, "Invalid Folder"
Me.lblFileCount.Caption = "Files Found: 0"
End If
' Clean up the FileSystemObject when the UserForm closes or after use (optional here, as it's global to the form)
' Set objFSO = Nothing ' If you want to release it after each search
Exit Sub
ErrorHandler:
' Handle errors, e.g., permission denied to access a folder
MsgBox "An error occurred while accessing folder: " & strFolderPath & vbCrLf & _
"Error: " & Err.Description, vbCritical, "Access Error"
Me.lblFileCount.Caption = "Error during search."
On Error GoTo 0 ' Disable error handling
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
' Clean up the FileSystemObject when the UserForm is closed
Set objFSO = Nothing
End Sub
' --- Code for Module1 (Standard Module) ---
' Place this code in a new standard module (e.g., Module1)
Option Explicit
' This function recursively gets all files within a given folder and its subfolders.
' It adds the full path of each file to the provided collection.
' Parameters:
' objFolder: The FileSystemObject.Folder object to process.
' colFiles: The Collection object to which file paths will be added.
Public Sub GetAllFiles(ByVal objFolder As Object, ByRef colFiles As Collection)
Dim objFile As Object
Dim objSubFolder As Object
On Error Resume Next ' Resume on error to handle inaccessible folders without stopping
' For example, System Volume Information or other protected folders.
' Add all files in the current folder to the collection
For Each objFile In objFolder.Files
colFiles.Add objFile.Path
Next objFile
' Recursively call this function for each subfolder
For Each objSubFolder In objFolder.SubFolders
Call GetAllFiles(objSubFolder, colFiles) ' Recursive call
Next objSubFolder
On Error GoTo 0 ' Re-enable normal error handling
End Sub
' This is a simple macro to display the UserForm.
' You can assign this macro to a button on your worksheet, or run it directly.
Public Sub ShowFileListForm()
frmFileList.Show
End Sub