Load folder names into ComboBox. My code is loading file names.

jmpatrick

Active Member
Joined
Aug 17, 2016
Messages
482
Office Version
  1. 365
Platform
  1. Windows
Good morning!

I'm using this code to load Folder names into a ComboBox:

VBA Code:
    Dim MyFile As String
    Dim MyPath  As String
    MyPath = "\\GLC-SERVER\Pulte\Z_Pulte Master Archive\"
    MyFile = Dir(MyPath)

    Do While MyFile <> ""
    Archive_Open_Year.AddItem MyFile
    MyFile = Dir
    Loop

Unfortunately it's only loading File names that exist in \\GLC-SERVER\Pulte\Z_Pulte Master Archive\. Can I make this load only Folder names that exist in \\GLC-SERVER\Pulte\Z_Pulte Master Archive\?

jp
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
I would suggest using FileSystemObject instead. It has the FolderExists method to check whether a folder exists, and you can easily loop through subfolders within a folder. And, unlike the Dir function, it won't error out when the specified network doesn't exist. Try something like this...

VBA Code:
    Dim fso As Object
    Dim MySubFolder As Object
    Dim MyPath  As String
    
    MyPath = "\\GLC-SERVER\Pulte\Z_Pulte Master Archive\"
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    If Not fso.FolderExists(MyPath) Then
        MsgBox "'" & MyPath & "' does not exist!", vbExclamation
        Exit Sub
    End If
    
    For Each MySubFolder In fso.GetFolder(MyPath).SubFolders
        Archive_Open_Year.AddItem MySubFolder.Name
    Next MySubFolder

Hope this helps!
 
Upvote 0
Solution
I would suggest using FileSystemObject instead. It has the FolderExists method to check whether a folder exists, and you can easily loop through subfolders within a folder. And, unlike the Dir function, it won't error out when the specified network doesn't exist. Try something like this...

VBA Code:
    Dim fso As Object
    Dim MySubFolder As Object
    Dim MyPath  As String
   
    MyPath = "\\GLC-SERVER\Pulte\Z_Pulte Master Archive\"
   
    Set fso = CreateObject("Scripting.FileSystemObject")
   
    If Not fso.FolderExists(MyPath) Then
        MsgBox "'" & MyPath & "' does not exist!", vbExclamation
        Exit Sub
    End If
   
    For Each MySubFolder In fso.GetFolder(MyPath).SubFolders
        Archive_Open_Year.AddItem MySubFolder.Name
    Next MySubFolder

Hope this helps!

Works great. Thanks!
 
Upvote 0
Write your data into your listbox in 1 go, using an array

VBA Code:
Private Sub UserForm_Initialize()
 Dim xp, fld, x As Long
 xp = "C:\Users\xxxx\Documents\"
 With CreateObject("scripting.filesystemobject")
    If .folderexists(xp) Then
      With .getfolder(xp)
         ReDim ar(.subfolders.Count)
         For Each fld In .subfolders
            ar(x) = fld.Name
            x = x + 1
         Next
      End With
     Archive_Open_Year.List = ar
    End If
 End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top