Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HOW TO SEARCH FOR FILES FROM VBA 2

Status
Not open for further replies.

Jdoggers

Technical User
Feb 20, 2005
43
US
Hi,
I would like to search through a specific folder on the hard drive (which i already know the path) and use code to search through every file in that directory and populate a dropdown combo box with the names of every file in the folder. Is there any way to do this with vba?

thanks for the help
 
Hello,

If you have XP or later this will work otherwise use the second one.
Code:
Sub ListFiles()
    Dim Path As String
    Dim fName As String
    Dim fd As FileDialog
    Set fd = Application.FileDialog(msoFileDialogFolderPicker)
        With fd
            .AllowMultiSelect = False
            If .Show = 0 Then
                Exit Sub
            End If
            Path = .SelectedItems(1)
        End With
        fName = Dir(Path & "\", vbNormal)
        Me.ComboBox1.Clear
        Do While CBool(Len(fName))
            Sheets("YourSheetName").YourComboBoxName.AddItem fName
            fName = Dir
        Loop
End Sub
Code:
Sub ListFiles()
    Dim Path As String
    Dim fName As String
        fName = Dir("C:\YourPath\", vbNormal)
        Me.ComboBox1.Clear
        Do While CBool(Len(fName))
            Sheets("YourSheetName").YourComboBoxName.AddItem fName
            fName = Dir
        Loop
End Sub

HTH,
Eric
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top