Steve.....this is how i would do it...noted the
use of array variants = fast way to access data
for writting back....hence the change in some of
the code.....which works pretty fast..rather then
looping through.
Option Explicit
Option Base 1
Function GetFileList(FileSpec As String) As Variant
' Returns an array of filenames that match FileSpec
' If no matching files are found, it returns False
Dim FileSearch
Dim FileArray() As Variant
Dim i As Double
On Error GoTo ErrSearch
Set FileSearch = Application.FileSearch
With FileSearch
.LookIn = FileSpec
.FileName = "*.xls"
If .Execute > 0 Then
ReDim FileArray(.FoundFiles.Count, 3)
For i = 1 To .FoundFiles.Count
FileArray(i, 1) = .FoundFiles(i)
FileArray(i, 2) = FileLen(.FoundFiles(i)) \ 1024 & " Kb"
FileArray(i, 3) = Format(FileDateTime(.FoundFiles(i)), "dd/mm/yy hh:mm:ss")
Next
Else
GetFileList = False
Exit Function
End If
End With
GetFileList = FileArray
Exit Function
' Error handler
ErrSearch:
MsgBox Err.Number & " : " & Err.Description, vbMsgBoxHelpButton, _
"Error Search", Err.HelpFile, Err.HelpContext
End
End Function
Sub test()
Dim p As String, x As Variant, i As Double
p = "C:\Temp"
x = GetFileList(p)
Select Case IsArray(x)
Case True 'files found
Sheets("Sheet1").Range("A:C").Clear
Sheets("Sheet1").Range("A1").Resize(UBound(x), 3) = x
Sheets("Sheet1").Range("A:C").Columns.AutoFit
MsgBox "Done!...." & UBound(x) & " files found"
Case False 'no files found
MsgBox "No matching files"
End Select
End Sub
HTH
Ivan
Slight adj - for error if Dir non existant
Option Explicit
Option Base 1
Function GetFileList(FileSpec As String) As Variant
' Returns an array of filenames that match FileSpec
' If no matching files are found, it returns False
Dim FileSearch
Dim FileArray() As Variant
Dim i As Double
Dim Exists
On Error GoTo ErrSearch
Set FileSearch = Application.FileSearch
Exists = Dir(FileSpec)
If Exists = "" Then GoTo ErrSearch
With FileSearch
.NewSearch
.LookIn = FileSpec
.FileName = "*.xls"
If .Execute > 0 Then
ReDim FileArray(.FoundFiles.Count, 3)
For i = 1 To .FoundFiles.Count
FileArray(i, 1) = .FoundFiles(i)
FileArray(i, 2) = FileLen(.FoundFiles(i)) \ 1024 & " Kb"
FileArray(i, 3) = Format(FileDateTime(.FoundFiles(i)), "dd/mm/yy hh:mm:ss")
Next
Else
GetFileList = False
Exit Function
End If
End With
GetFileList = FileArray
Set FileSearch = Nothing
Exit Function
' Error handler
ErrSearch:
If Exists = "" Then On Error Resume Next: Err.Raise 76
MsgBox Err.Number & " : " & Err.Description, vbMsgBoxHelpButton, _
"Error Search", Err.HelpFile, Err.HelpContext
End
End Function
Sub test()
Dim p As String, x As Variant, i As Double
p = "C:\a" ' "C:\ExcelFiles\Useful\"
x = GetFileList(p)
Select Case IsArray(x)
Case True 'files found
Sheets("Sheet1").Range("A:C").Clear
Sheets("Sheet1").Range("A1").Resize(UBound(x), 3) = x
Sheets("Sheet1").Range("A:C").Columns.AutoFit
MsgBox "Done!...." & UBound(x) & " files found"
Case False 'no files found
MsgBox "No matching files"
End Select
x = ""
End Sub