How to display a list of all files in a folder in a combo box on user-form with VBA. We will use the ‘FileSystemObject‘ or FSO. By creating an instance of the FileSystemObject in Excel VBA, we can access files and folders on our computer system. We are now able to manipulate the files like reading, deleting or looping through them. We can also perform the following actions using the FileSystemObject (FSO):
- We can check for the existence of the file or folder
- We can copy files from one folder to another
- We can create new folders
- We can rename the files and folders
- We can get the list of all files in a folder or sub-folder
How to enable the FileSystemObject quickly and easily: Go to Tools in the Visual Basic Editor and set the reference to the ‘Microsoft Scripting Runtime’. Watch the video below to learn more:
Watch the video on YouTube.
Here’s the complete VBA code to show all the files in a folder in a combo-box on a user-form.
Private Sub UserForm_Initialize()
Dim myfso As FileSystemObject, myfolder As Object, myfile As Object
Set myfso = New Scripting.FileSystemObject
Set myfolder = myfso.GetFolder(“C:\invoices”)
For Each myfile In myfolder.Files