can i get a list of files in specified folder?

merlin_the_magician

Active Member
Joined
Jul 31, 2002
Messages
480
In order to create an external cell reference that will not result in highly annoying errors I need a formula or VBA code that will allow me to retrieve the filenames in a specified path. I know this is possible, just how?
 
It works fine now. I forgot to actually run the macro. (talking stupid....):D

One more thing, can i have this run automatically at file opening?
 
Upvote 0

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Yeah,

place the code in the This Workbook code pane

Replace the name of the macro as per below

Code:
Private Sub Workbook_Open()
    Dim fileList() As String
    Dim fName As String
    Dim fPath As String
    Dim I As Integer
   
    fPath = "C:\My Documents\"
    fName = Dir(fPath & "*.xls")
    While fName <> ""
        I = I + 1
        ReDim Preserve fileList(1 To I)
        fileList(I) = fName
        fName = Dir()
    Wend
    If I = 0 Then
        MsgBox "No files found"
        Exit Sub
    End If
     For I = 1 To UBound(fileList)
        Range("A" & I).Value = fileList(I)
     Next
End Sub

Will[/code]
 
Upvote 0

Forum statistics

Threads
1,217,323
Messages
6,135,899
Members
449,968
Latest member
Bpc1284

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