View Single Post
  #8   Report Post  
Posted to microsoft.public.excel.misc
Jim Rech Jim Rech is offline
external usenet poster
 
Posts: 2,718
Default Importing Text files to Excel 2007

Here's two ways to drill into subfolders:

1. Requires a reference to MS Scriptiong Runtime:

Option Compare Text

Sub ListXLFiles()
Dim FilesCollection As New Collection, Counter As Long
FindFiles "c:\excel", "*.xls", FilesCollection, True
Sheet1.Columns(1).Clear
For Counter = 1 To FilesCollection.Count
Sheet1.Cells(Counter, 1).Value = FilesCollection(Counter)
Next
End Sub

Sub FindFiles(FolderName As String, FileSpec As String, Col As Collection,
Recurs As Boolean)
Dim fso As Scripting.FileSystemObject
Dim fld As Scripting.Folder
Dim fldSub As Scripting.Folder
Dim fle As Scripting.file
Set fso = New Scripting.FileSystemObject
Set fld = fso.GetFolder(FolderName)
For Each fle In fld.Files
If fle.Name Like FileSpec Then Col.Add fle.Path
Next
If Recurs Then
For Each fldSub In fld.SubFolders
FindFiles fldSub.Path, FileSpec, Col, True
Next
End If
Set fso = Nothing
End Sub

2. Uses FileSearch which I believe is not in Excel 2007.

Sub FileSearchList()
Dim i As Integer
Sheet1.Columns(2).Clear
With Application.FileSearch
.NewSearch
.LookIn = "c:\excel"
.SearchSubFolders = True
.Filename = "*.xls"
.FileType = msoFileTypeExcelWorkbooks
If .Execute() 0 Then
For i = 1 To .FoundFiles.Count
Sheet1.Cells(i, 2).Value = .FoundFiles(i)
Next i
Else
MsgBox "There were no files found."
End If
End With
End Sub



--
Jim
"RobN" wrote in message
...
Jim,

Could you also advise what the code would be if I wanted to include the
files within any subfolders?

Rob

"Jim Rech" wrote in message
...
You can get a quick list of the files in a folder with this macro. Put
the folder path in cell A1.

Sub a()
Dim FilePath As String
Dim FName As String
Dim Counter As Integer
FilePath = Range("A1").Value
If Right(FilePath, 1) < "\" Then FilePath = FilePath & "\"
FName = Dir(FilePath & "*.*", vbNormal)
Do While FName < ""
Counter = Counter + 1
Cells(Counter + 1, 1).Value = FName
FName = Dir
Loop
End Sub


--
Jim
"Quco" wrote in message
...
Marvin,

Back in 1986 I learned a little bit of MS-DOS commands with an Atari XT
computer I used just for fun, but I can't remember how to use these
commands
now.

This is what I have done so far:

1. File Run cmd
2. Enter G: to switch to the location of the hard drive

Now, the actual path for the folder containing all these files is:
G:\Gulf

Unfortunately I can't get in the Gulf folder to try the command "dir/s
files.txt"
I do not understood the rest of your instructions. Could you please
clarify?