Results 1 to 3 of 3

Thread: Can someone tell me what is the problem

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2000
    Posts
    15

    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

  2. #2
    Addicted Member eer3's Avatar
    Join Date
    Sep 2000
    Location
    Ca
    Posts
    165
    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

  3. #3
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    When you use Preserve with Redim, you can only resize the last dimension.

    As quoted by MSDN.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width