find all text files in a directory

S

strataguru

Hi,

I'm trying to find code that allows me:

1) Ask the user to select a directory - not file - a directory tha
has files that need to be imported
2) Open, one at a time, text files in that directory

Any ideas?

Thanks
 
B

Bob Phillips

Try this

Sub ProcessFiles()
Dim i As Long
Dim sFolder As String
Dim fldr As Object
Dim Folder As Object
Dim file As Object
Dim Files As Object

Set FSO = CreateObject("Scripting.FileSystemObject")

'sFolder = "C:\myTest"
sFolder = InputBox("Supply folder")
if sFolder = "" then Exit Sub
If sFolder <> "" Then
Set Folder = FSO.GetFolder(sFolder)

Set Files = Folder.Files
For Each file In Files
If file.Type = "Text Document" Then
Workbooks.OpenText Filename:=file.path, _
Origin:=xlMSDOS, _
StartRow:=1, _
DataType:=xlDelimited, _
ConsecutiveDelimiter:=True, _
Space:=True
'>>>>>>>>>> add your processing here
End If
Next file

End If ' sFolder <> ""

End Sub



--

HTH

RP
(remove nothere from the email address if mailing direct)
 
S

strataguru

Is there anyway to use a Microsoft tool that is similiar to a Microsof
Explorer window where the user can select the folder rather than rel
on the user to correctly type in the full path
 

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