Okay, then this might be the solution;
As the code runs, messages will appear in the Immediate Window. Pay close attention to: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 Dim i As Long ' Counter for listbox population 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 ' Temporarily remove On Error GoTo ErrorHandler here to allow more specific errors to show ' We will rely on the error handling within GetAllFiles for specific folder access issues. ' On Error GoTo ErrorHandler ' Re-enable if you want overall UserForm error handling Set objRootFolder = objFSO.GetFolder(strFolderPath) ' DEBUG: Print the root folder being processed Debug.Print "Starting file search in: " & strFolderPath ' Call the recursive function to populate the collection with file paths Call GetAllFiles(objRootFolder, colFiles) ' DEBUG: Print the total count of files collected Debug.Print "Total files collected into collection: " & colFiles.Count ' Populate the ListBox with the found file paths i = 0 For Each varFile In colFiles Me.lstFiles.AddItem varFile i = i + 1 ' Allow UI to update for large lists If i Mod 100 = 0 Then DoEvents 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: ' Only active if On Error GoTo ErrorHandler is enabled above ' 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) 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 Dim initialFileCount As Long ' For debugging ' DEBUG: Print the folder currently being processed Debug.Print " Processing folder: " & objFolder.Path initialFileCount = colFiles.Count ' Store count before adding files from this folder ' Use specific error handling for folder access issues On Error GoTo FileAccessError ' Add all files in the current folder to the collection For Each objFile In objFolder.Files colFiles.Add objFile.Path Next objFile ' DEBUG: Print how many files were added from this specific folder Debug.Print " Files added from '" & objFolder.Name & "': " & (colFiles.Count - initialFileCount) ' 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 Exit Sub ' Exit the sub if no errors FileAccessError: ' DEBUG: Log the error and the folder that caused it Debug.Print " ERROR accessing folder: " & objFolder.Path & " - " & Err.Description Resume Next ' Continue to the next item or exit the loop/function 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
Starting file search in: [Your Folder Path]
Processing folder: [Specific Folder Path] (for each folder scanned)
Files added from '[Folder Name]': [Number] (This tells you how many files were successfully added from each individual folder).
ERROR accessing folder: [Folder Path] - [Error Description] (If there are permission issues).
Total files collected into collection: [Final Count]
If the "Files added from" line shows 0 for all folders, and the "Total files collected" also shows 0, it's highly likely that:
Permissions: Your user account doesn't have read access to the files or folders in that directory structure. This is a common issue with system folders or network drives.
No Actual Files: Though less likely, the folders might genuinely be empty of files (they might contain only subfolders).
Please let me know what output you see in the Immediate Window. That will give us a much clearer picture of what's happening!




Reply With Quote