Announcement

Collapse
No announcement yet.

Add files in directory to a listbox

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Add files in directory to a listbox

    hello,


    I want to show the files from a directory in a listbox by pressing a button.
    is there a short and understandable code for this??


    thanks for your help

  • #2
    Re: Files in directory

    What do you want to be able to do once you have them in your listbox ? The reason I ask is that if you want to be able to open them, it would be simpler to use the GetOpenFileName dialog box

    Code:
    chdir("C:\Will\") :application.GetOpenFilename
    Will show the OpenFile dialog box, containing a list of all files in the directory C:\Will\
    Kind Regards, Will Riley

    LinkedIn: Will Riley

    Comment


    • #3
      Re: Files in directory

      If you want to add the files to a listbox on a userform, places the following in a userform initialise event

      Code:
      Private Sub UserForm_Initialize()
          Dim fileList() As String
          Dim fName As String
          Dim fPath As String
          Dim I As Integer
          'define the directory to be searched for files
          fPath = "C:\Will\"
          
          'build a list of the files
          fName = Dir(fPath & "*.xls")
          While fName <> ""
              'add fName to the list
              I = I + 1
              ReDim Preserve fileList(1 To I)
              fileList(I) = fName
              'get next filename
              fName = Dir()
          Wend
          'see if any files were found
          If I = 0 Then
              MsgBox "No files found"
              Exit Sub
          End If
          'cycle through the list and add to listbox
          For I = 1 To UBound(fileList)
          Me.ListBox1.AddItem fileList(I)
          Next
      End Sub
      Kind Regards, Will Riley

      LinkedIn: Will Riley

      Comment


      • #4
        Re: Add files in directory to a listbox

        that last one was good enough for me.

        thanx

        Comment

        Working...
        X