put all files in directory in listbox on userform

birdman

Board Regular
Joined
Oct 11, 2005
Messages
187
i have a macro which opens a userform. when the userform appears i would like all the files in a certain directory to appear in a listbox that is on the userform. does anyone know the code, and where to put the code to do this?? thanks for any help in advance.
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Steve

This is certainly possible, and I'll try and find some appropriate code, but have you considered using the GetOpenFileName method?

Here's some code.
Code:
Private Sub UserForm_Initialize()
Dim I As Long

    With Application.FileSearch
        .NewSearch
        .LookIn = "C:\"
        .FileType = msoFileTypeExcelWorkbooks
        .Execute
        For I = 1 To .FoundFiles.Count
            ListBox1.AddItem .FoundFiles(I)
        Next I
    End With
End Sub
 
Upvote 0
i am currently using the GetOpenFileName method. i wish to change to this so that when the userform opens, all the files in the folder are shown in the listbox without having to deal with selecting the files.
 
Upvote 0
i notice it says
Code:
.FileType = msoFileTypeExcelWorkbooks
every file in the folder is going to be a text file. i am not sure to put in replace of this line.

thanks for all your help by the way
 
Upvote 0
Maybe this?
Code:
Private Sub UserForm_Initialize()
Dim I As Long

    With Application.FileSearch
        .NewSearch
        .LookIn = "C:\"
        .FileType = msoFileTypeAllFiles
        .Filename = "*.txt" ' assumes the text files have the txt extension
        .Execute
        For I = 1 To .FoundFiles.Count
            ListBox1.AddItem .FoundFiles(I)
        Next I
    End With
End Sub
By the way why do you not want to use the GetOpenFileName method?
 
Upvote 0

Forum statistics

Threads
1,214,790
Messages
6,121,607
Members
449,037
Latest member
Arbind kumar

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