• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

select folder in list box 1 and show files of this folder in listbox 2 userform

haai

New Member
I have a userform with 2 listboxes.
I populate the first listbox with the subfolders in the same folder where de excel file is.
Code:
Private Sub UserForm_Initialize()
Set FileSystem = CreateObject("Scripting.FileSystemObject")
    For Each SubFolder In FileSystem.GetFolder(hostFolder).SubFolders
        ListBox1.AddItem SubFolder.Name
    Next
   Dim File
    For Each File In FileSystem.GetFolder(hostFolder).Files
    If Right(File.Name, 5) = ".PDF" Then
            ListBox1.AddItem File.Name
    End If
    Next
End Sub
This works

Now I would like to click in Listbox 1 to show the PDF files of the selected folder.in Listbox2
Code:
Private Sub ListBox1_Click()
  MyFolder = ThisWorkbook.Path & ListBox1.Column(0)
    MyFile = Dir(MyFolder & "\*.PDF")
        Do While MyFile <> ""
            ListBox2.AddItem MyFile
        MyFile = Dir
Loop
End Sub
This doesnt'work.
Anybody suggestions?
Thanks in advance.
 
Hi Haai,

A single change is required in first line. I added "\"

Code:
  MyFolder = ThisWorkbook.Path & "\" & ListBox1.Column(0)
  myFile = Dir(MyFolder & "\*.PDF")
    Do While myFile <> ""
        ListBox2.AddItem myFile
        myFile = Dir
    Loop

Thanks,
Saurabh
 
Back
Top