Userform - Drop down list txt files in a specified path?

largeselection

Active Member
Joined
Aug 4, 2008
Messages
358
Hi,

So I have some code and in the workbook I define a path where a number of text files are saved.

I have a userform which I would like to use to select which text file to reference. I have used other code/userforms in the past to select which open workbook I want to use.

What I want to know is how can I have the userform dropdown display the names of the text files in the folder at the path I specify?

Here is the code I have used to display the list of open workbooks in the userform in the past:

Code:
Private Sub UserForm_Initialize()
    Me.Label1.Caption = "Please select one of the following files..."
    With Me.ComboBox1
        For Each wkb In Application.Workbooks
            .AddItem wkb.Name
        Next wkb
    End With
End Sub

Any ideas or suggestions are much appreciated!

Thanks!
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Assuming that A1 on Sheet1 contains the path to your folder, try...

Code:
[font=Verdana][color=darkblue]Private[/color] [color=darkblue]Sub[/color] UserForm_Initialize()
    [color=darkblue]Dim[/color] strPath [color=darkblue]As[/color] [color=darkblue]String[/color]
    [color=darkblue]Dim[/color] strFile [color=darkblue]As[/color] [color=darkblue]String[/color]
    [color=darkblue]Dim[/color] Cnt [color=darkblue]As[/color] [color=darkblue]Long[/color]
    strPath = Worksheets("Sheet1").Range("A1")
    [color=darkblue]If[/color] Right(strPath, 1) <> "\" [color=darkblue]Then[/color] strPath = strPath & "\"
    Me.Label1.Caption = "Please select one of the following files..."
    strFile = Dir(strPath & "*.txt")
    [color=darkblue]Do[/color] [color=darkblue]While[/color] Len(strFile) > 0
        [color=darkblue]With[/color] Me.ComboBox1
            .AddItem strFile
        [color=darkblue]End[/color] [color=darkblue]With[/color]
        strFile = Dir
    [color=darkblue]Loop[/color]
[color=darkblue]End[/color] [color=darkblue]Sub[/color][/font]
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,348
Members
452,907
Latest member
Roland Deschain

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