VBA Code to List Folders in a Directory

rhouston08

New Member
Joined
Nov 25, 2013
Messages
21
I haven't used VBA in about 2 years, and I lost my logical mindset, when it comes to the syntax for programming in Excel. Anywho, I have a make shift code for renaming folder(s), but I need help with VBA to allow me to choose a folder (not subfolder), preferrably in a window, in a specific location, and then just list he folders (and not subfolders) in a spreadsheet. Any assistance is appreciative. Thanks!
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
The following macro will prompt the user to select a directory, and then list the folders within the directory in a newly created worksheet...

Code:
Option Explicit

Sub ListFoldersInDirectory()


    Dim objFSO As Object
    Dim objFolders As Object
    Dim objFolder As Object
    Dim strDirectory As String
    Dim arrFolders() As String
    Dim FolderCount As Long
    Dim FolderIndex As Long


    With Application.FileDialog(msoFileDialogFolderPicker)
        .InitialFileName = Application.DefaultFilePath & "\"
        .Title = "Select Folder"
        .Show
        If .SelectedItems.Count = 0 Then
            Exit Sub
        End If
        strDirectory = .SelectedItems(1)
    End With
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolders = objFSO.GetFolder(strDirectory).SubFolders
    
    FolderCount = objFolders.Count
    
    If FolderCount > 0 Then
        ReDim arrFolders(1 To FolderCount)
        FolderIndex = 0
        For Each objFolder In objFolders
            FolderIndex = FolderIndex + 1
            arrFolders(FolderIndex) = objFolder.Name
        Next objFolder
        Worksheets.Add
        Range("A1").Resize(FolderCount).Value = Application.Transpose(arrFolders)
    Else
        MsgBox "No folders found!", vbExclamation
    End If
    
    Set objFSO = Nothing
    Set objFolders = Nothing
    Set objFolder = Nothing
    
End Sub

Hope this helps!
 
Upvote 0
Hello rhouston08,

Here is another method using the Shell Application Object.

Code:
Sub ListSubFolders()


    Dim Cell        As Range
    Dim Folder      As Variant
    Dim SubFolders  As Variant
    Dim vArray      As Variant
    
        Set Cell = Range("A1")
        
        With Application.FileDialog(msoFileDialogFolderPicker)
            If .Show Then Folder = .SelectedItems(1) Else Exit Sub
        End With
        
        With CreateObject("Shell.Application")
            Set SubFolders = .Namespace(Folder).Items
                SubFolders.Filter 32, "*"
        End With
        
        If SubFolders.Count = 0 Then
            MsgBox "There are No Subfolders in this Directory."
            Exit Sub
        End If
        
        ReDim vArray(1 To SubFolders.Count, 1 To 1)
        
        For n = 0 To SubFolders.Count - 1
            vArray(n + 1, 1) = SubFolders.Item(n).Name
        Next n
        
        With Cell.Resize(n, 1)
            .NumberFormat = "@"
            .Value = vArray
        End With
        
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,329
Members
452,635
Latest member
laura12345

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