|
-
Jun 6th, 2001, 03:14 PM
#1
Thread Starter
New Member
Can someone tell me what is the problem
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
-
Jun 6th, 2001, 03:26 PM
#2
Addicted Member
First, your creating a multi-dimensional array with
ReDim files(1 To 100, 2)
then your trying to redimension and preserve a single dimension array...
ReDim Preserve files(1 To lfcount + 100)
to fix the problem, keep it multi-dimensional or lose the preserve command
-
Jun 6th, 2001, 03:46 PM
#3
Well ...
When you use Preserve with Redim, you can only resize the last dimension.
As quoted by MSDN.
.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|