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
744 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article describes a serious pitfall that can happen when deleting shapes using VBA.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

632 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