Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / New Users / July 2007

Tip: Looking for answers? Try searching our database.

list box question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Joanne - 25 Jul 2007 23:57 GMT
Can I populate my list box with the files named in a sub-directory on my
hard drive, and if yes, when I click on one of these file names can I
open the file programmatically directly from the on click event of the
listbox?

Thanks for your help
Joanne
Bernie Deitrick - 26 Jul 2007 13:41 GMT
Joanne,

Populate the listbox and show the userform:

Sub GetFiles()
Dim myArray() As String
With Application.FileSearch
  .NewSearch
  .LookIn = "C:\Excel"
  .FileName = "*.xls"
  If .Execute() > 0 Then
     ReDim myArray(0 to .FoundFiles.Count - 1)
     For i = 0 To .FoundFiles.Count - 1
        myArray(i) = .FoundFiles(i + 1)
     Next i
     Load Userform1

     Userform1.ListBox1.List = myArray
     Userform1.ListBox1.Selected(0) = False
     Userform1.Show
  End If
End With
End Sub

Then, to open the file when you click it:

Private Sub ListBox1_Change()
Userform1.Hide
Application.Workbooks.Open Userform1.ListBox1.List(Userform1.ListBox1.ListIndex)
Unload Userform1
End Sub

HTH,
Bernie
MS Excel MVP

> Can I populate my list box with the files named in a sub-directory on my
> hard drive, and if yes, when I click on one of these file names can I
[quoted text clipped - 3 lines]
> Thanks for your help
> Joanne

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.