Generate list of folders in given Dir

J

john q public

Hi all i am trying to fill a combobox with the names of folders in 26
different directories using the following code


MyPath = "q:\orders\a\" ' Set the path.
myname = Dir(MyPath, vbDirectory) ' Retrieve the first entry.
Do While myname <> "" ' Start the loop.
' Ignore the current directory and the encompassing directory.
If myname <> "." And myname <> ".." Then
ComboBox1.AddItem myname
If (GetAttr(MyPath & myname) And vbDirectory) = vbDirectory
Then


End If
End If
myname = Dir Loop

my path canges from a,b,c,d,e,f..........z and the code i have here
does the job however it takes about 1 minute for it to run all the
loops and fill the combobox.

Is there anyway this can be done quicker?
 
T

Tom Ogilvy

Set a reference to the Microsoft Scripting Runtime:

Sub Tester1()
Dim fso As New FileSystemObject
Dim fld As Folder
Dim tFld As Folder
Dim sFol As String
sFol = "C:\"
Set fld = fso.GetFolder(sFol)
i = 0
For Each tFld In fld.SubFolders
i = i + 1
Cells(i, 1).Value = tFld.Name
Next
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top