Thursday, February 9, 2012

GetFileList function

Function GetFileList(filespec As String) As Variant
'***Function to get list of files in specific folder***'
' Returns an array of file names that match FileSpec
' If no matching files are found, it returns False

Dim FileArray() As Variant
Dim FileCount As Integer
Dim FileName As String
On Error GoTo NoFilesFound

FileCount = 0
FileName = Dir(filespec)
If FileName = "" Then GoTo NoFilesFound
' Loop until no more matching files are found
Do While FileName <> ""
   FileCount = FileCount + 1
   ReDim Preserve FileArray(1 To FileCount)
   FileArray(FileCount) = FileName
   FileName = Dir()
Loop
GetFileList = FileArray
Exit Function

' Error handler
NoFilesFound:
GetFileList = False
End Function

No comments:

Post a Comment