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
449 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:
SimonAdept 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Outlook Free & Paid Tools
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now