Any suggestions as to how to go about populating a listbox with the names of all the files in a specified folder:
("C:\Dropbox" that have the file extension .xls ?
Dim fso As FileSystemObject
Dim fl As File
Dim str as String
Set fso = CreateObject("Scripting.FileSystemObject"
For Each fl In fso.GetFolder("C:\Dropbox".Files
If Right(fl.Name, 3) = "xls" Then
str = Left(fl.Name, InStr(1, fl.Name, "." - 1)
If InStr(1, Form_what.Combo9.RowSource, str) Then
Else
Form_<frmname>.<listboxctrl>.RowSource = Form_<frmname>.<listboxctrl>.RowSource & ";""" & str & """"
End If
End If
Next fl
'If Not Instr(...) Then' didn't work. That's why I said 'If Instr(...) Then Else'.
FWIW: The reason the not instr() didn't work is because instr returns an integer of the position of the searched for text. If it doesn't find it then it returns 0.
Ben ----------------------------------
Ben O'Hara
bo104@westyorkshire.pnn.police.uk
----------------------------------
I just realized a potential bug. The code adds the filename to the source only if the name isn't already somewhere in the source. If one filename exists in other filenames, it may not be added. For example, consider this.xls and thisone.xls. If "thisone" is already in the source, "this" won't be added. Potential problem depending on filenames. Just letting you know.
This code is a little more cunning, and easier to use.
rootPath is just used in order to use the method in conjunction with the 'OnClick' method of the listbox...
Dim rootPath as string
Private Sub UserForm_Activate()
rootPath = "C:\Dropbox\"
dirName = Dir(rootPath & "*.xls", vbNormal)
While dirName <> ""
Me.listBox1.AddItem dirName
dirName = Dir()
Wend
End Sub
Hope this helps (and you're still looking at this post!)
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.