Solved

Excel VBA list the folders in a directory in an array to perform a task with each one

Posted on 2015-02-03
2
598 Views
Last Modified: 2016-02-10
I am trying to run some code on a change event, when someone changes a drop down, I need this sub to go off, list all the folders in a specific directory by name only in an array. So I can perform a task then on each item in the array.

Is there a function someone has already written to do this? Or an efficient way of doing this?

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Excel.Range)
Dim bTest As Boolean
Dim wb As Workbook
Dim MyArray as variant


If Target.Column = "3" Then
    bTest = Target.Row Mod 2 = 0
    If bTest = True And Target.Row > 3 And Target.Row < 39 Then
' Here I need to get array containing all folder names in a directory. (Don't need their subfolders)
    'MyArray = Function To Get Folder Names Back Into The Array

    Else: End If
Else: End If

 
 
 End Sub

Open in new window

0
Comment
Question by:Conor_Newman
2 Comments
 
LVL 12

Expert Comment

by:James Elliott
ID: 40586787
Here's a great tutorial of the DIR function.

http://www.exceltrick.com/formulas_macros/vba-dir-function/
0
 
LVL 18

Accepted Solution

by:
Simon earned 500 total points
ID: 40586792
You can do this with the FileSystemObject. This example gets all the subfolders of the chosen basefolder - in this case "C:\" - and adds them to array, then lists the array elements.
Sub test()
Dim arrFolders() As String
Dim ctr As Integer

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\")
Set colSubfolders = objFolder.Subfolders
For Each objsubfolder In colSubfolders
    ctr = ctr + 1
    ReDim Preserve arrFolders(ctr)
    arrFolders(ctr) = objsubfolder.Name
Next

'print each item from the array
For ctr = LBound(arrFolders) To UBound(arrFolders)
    Debug.Print ctr, arrFolders(ctr)
Next
End Sub

Open in new window

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question