Reading Directories

S

STe

Hi,

I would like to be able to loop through files within a given directory and
return the name of the files from within VBA Excel. The file types within the
directory will be mixed, however they will be mainly text files. I am not
sure if this is possible. I hope that my question makes sense. TIA.

Regards

Steven
 
D

Don Guillett

One way of several. You can restrict file type such as *.xls

Sub GetFileList()
Dim ThePath As String
Dim fname As String
Dim i As Long
ThePath = "c:\A" 'ThisWorkbook.Path
fname = Dir(ThePath & "\*.*")
i = 1
Do While fname <> ""
On Error Resume Next
Cells(i, "a") = fname
'MsgBox fname
On Error GoTo 0
fname = Dir()
i = i + 1
Loop
End Sub
 
S

STe

Thank you Don! That is a great help.


Don Guillett said:
One way of several. You can restrict file type such as *.xls

Sub GetFileList()
Dim ThePath As String
Dim fname As String
Dim i As Long
ThePath = "c:\A" 'ThisWorkbook.Path
fname = Dir(ThePath & "\*.*")
i = 1
Do While fname <> ""
On Error Resume Next
Cells(i, "a") = fname
'MsgBox fname
On Error GoTo 0
fname = Dir()
i = i + 1
Loop
End Sub

--
Don Guillett
Microsoft MVP Excel
SalesAid Software
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top