Hi there,

Can please tell me what is the problem with my code here. Basically I am trying to add the path of the file and name of the file to a 2 dimensional array. But I get the error "Subscript out of Range" and it points to the line "Redim Preserve" before the end function.

Here is the code:

code:---------------------------------------------------------------
Function GetFiles(ByVal DirPath As String, Optional ByVal Ext As String) As Variant

Dim files() As String
ReDim files(1 To 100, 2)
Dim fname
Dim lfcount

GetFiles = Null

If Right(DirPath, 1) <> "\" Then DirPath = DirPath & "\"

If Len(Dir$(DirPath, vbDirectory)) Then

fname = Dir$(DirPath & "*.*", vbNormal + vbHidden + vbSystem)
Do While Len(fname)
If Len(Ext) = 0 Or (Len(Ext) And Right(fname, Len(fname) - InStr(1, fname, ".")) = Ext) Then
lfcount = lfcount + 1
files(lfcount, 1) = DirPath & fname
files(lfcount, 2) = fname
If lfcount Mod 100 = 0 Then ReDim Preserve files(1 To lfcount + 100)
End If
fname = Dir$
Loop

If lfcount Then ReDim Preserve files(1 To lfcount)
GetFiles = files

End If
End Function
end code--------------------------------------------------------------
Thanks in advance,
Radhika