Consulting

Results 1 to 5 of 5

Thread: Solved: List all files in folders

  1. #1
    VBAX Contributor
    Joined
    Apr 2006
    Posts
    144
    Location

    Solved: List all files in folders

    Hello everyone,

    I'm sure there are many ways to list files in a directory using VBA. The following code do the same thing except it misses the first file. I have a mental blank at the moment. Would someone show me what's wrong with it please. Thanks in advance for your responses.

    [vba]Option Explicit
    Option Base 0
    Public Function AllFiles(ByVal DirPath As String) As String()
    Dim sFile As String
    Dim lElement As Long
    Dim sAns() As String
    ReDim sAns(0) As String
    sFile = Dir(DirPath)
    If sFile <> "" Then
    sAns(0) = sFile
    Do
    sFile = Dir
    If sFile = "" Then Exit Do
    lElement = IIf(sAns(0) = "", 0, UBound(sAns) + 1)
    ReDim Preserve sAns(lElement) As String
    sAns(lElement) = sFile
    Range("A" & Cells.Rows.Count).End(xlUp)(2, 1) = sAns(lElement)
    Loop
    End If
    AllFiles = sAns
    MsgBox Range("A2").CurrentRegion.Rows.Count ' Returning the number of files in folder
    End Function
    [/vba]


    Regards


    kp

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Public Function AllFiles(ByVal DirPath As String) As String()
    Dim sFile As String
    Dim lElement As Long
    Dim sAns() As String
    ReDim sAns(0) As String
    sFile = Dir(DirPath, vbNormal)
    If sFile <> "" Then
    sAns(0) = sFile
    Do
    Range("A" & Cells.Rows.Count).End(xlUp)(2, 1) = sAns(lElement)
    sFile = Dir
    If sFile = "" Then Exit Do
    lElement = IIf(sAns(0) = "", 0, UBound(sAns) + 1)
    ReDim Preserve sAns(lElement) As String
    sAns(lElement) = sFile
    Loop
    End If
    AllFiles = sAns
    MsgBox Range("A2").CurrentRegion.Rows.Count ' Returning the number of files in folder
    End Function
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location
    [VBA]Public Function AllFiles(ByVal DirPath As String) As String()
    Dim sFile As String
    Dim lElement As Long
    Dim sAns() As String
    sFile = Dir(DirPath)
    Do While sFile <> ""
    lElement = lElement + 1
    ReDim Preserve sAns(lElement - 1)
    sAns(lElement - 1) = sFile
    Range("A" & Cells.Rows.Count).End(xlUp)(2, 1) = sAns(lElement - 1)
    sFile = Dir
    Loop
    AllFiles = sAns
    MsgBox Range("A2").CurrentRegion.Rows.Count ' Returning the number of files in folder
    End Function[/VBA]

  4. #4
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Remove this.
    [vba]
    lElement = IIf(sAns(0) = "", 0, UBound(sAns) + 1) [/vba]
    And add this before Loop.
    [vba]
    lElement = lElement+1
    [/vba]

  5. #5
    VBAX Contributor
    Joined
    Apr 2006
    Posts
    144
    Location
    Hi Bob, Charlize & Norie,

    Thank you so much for help in debugging my code. They now work beautifully.

    Have a great evening.

    My best regards


    kp

Posting Permissions

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