?
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
Medium Priority
?
830 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 2000 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

Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

Cancel future meetings from user mailboxes in Office 365 using Remove-CalendarEvents
Microsoft has changed the look and feel of Azure AD and Microsoft account sign-in pages so that you will have a more unified look and feel when moving between the two interfaces.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
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…

719 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