Getting file names in a directory

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hello,
Would anyone be able to tell me how to get a list of files
in a particular directory within a VBA routine in Excel
2003?

Thanks!
 
Try:

Sub xz()
Dim Files As Variant
Files = ""
Set fso = CreateObject("Scripting.FileSystemObject")
For Each File In fso.GetFolder("C:\AJAY").Files
Files = Files & "," & File.Name
Next
Files = Split(Mid(Files,2), ",")
Set fso = NOTHING
End Sub

NOTE:
1. Files is a zero-based variant array irrespective of your option base
setting.
2. This does NOT recurse flders below the one you specify, if any.
 
try
sub anotherfindfiles()
Application.ScreenUpdating = False
Dim FN As String ' For File Name
Dim ThisRow As Long
Dim FileLocation As String
FileLocation = "c:\myfolder\*.xls"

FN = Dir(FileLocation)
Do Until FN = ""
ThisRow = ThisRow + 1
Cells(ThisRow, 1) = FN
FN = Dir
Loop
Application.ScreenUpdating = True
End Sub
 

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

Back
Top