VBA to list all subfolders

msleo16

New Member
Joined
Oct 8, 2021
Messages
5
Office Version
  1. 365
Platform
  1. Windows
I have a network folder that has about 60 subfolders. Each subfolder have a tree of 6 subfolders, then those subfolders have about 5 more subfolders after that.
In other words the network folder can have 5-10 or more degrees of subfolders.
e.g.
L:\Country\New York\
L:\Country\New York\Bronx\
L:\Country\New York\Bronx\Business1
L:\Country\New York\Bronx\Business1\Employees\Fulltime\
L:\Country\New York\Bronx\Business1\Employees\Fulltime\Salary\
L:\Country\New York\Bronx\Business1\Employees\Partime\Salary\
L:\Country\New York\Bronx\Business1\History\
L:\Country\New York\Bronx\Business1\Locations\

I need a VBA to list all these degrees of subfolders.
Help!
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Open a new file and put this in a module. Run the code and you have list of folders (without files)

VBA Code:
Sub jvr()
a = Split(CreateObject("wscript.shell").Exec("cmd /c Dir ""L:\Country\New York\"" /s/b/a:d").StdOut.ReadAll, vbCrLf)
If IsArray(a) Then Cells(1, 1).Resize(UBound(a)) = Application.Transpose(a)
End Sub
 
Upvote 0
Hi thank you for responding.
to test, I updated the path to a local drive. when I run, I get "Run-time error '13': Type mismatch.
when I click on DeBug, "Cells(1, 1).Resize(UBound(a)) = Application.Transpose(a)" is highlighted in yellow.

pls advise, and thanks again for your help
 
Upvote 0
Could you post the code you used?
 
Upvote 0
a = Split(CreateObject("wscript.shell").Exec("cmd /c Dir ""C:\Users\user\Documents\Adobe\"" /s/b/a:d").StdOut.ReadAll, vbCrLf)
If IsArray(a) Then Cells(1, 1).Resize(UBound(a)) = Application.Transpose(a)
 
Upvote 0
in below line "user" needs to be actual user name
a = Split(CreateObject("wscript.shell").Exec("cmd /c Dir ""C:\Users\user\Documents\Adobe\"" /s/b/a:d").StdOut.ReadAll, vbCrLf)
 
Upvote 0
yes, that is what I did...i should've been more clear that for privacy, i replaced that with 'user' but the actual code had my actual username
 
Upvote 0
Try using UBound(a) > 0 instead of IsArray(a):
VBA Code:
Sub Test()
    Dim a, s As String
    s = "C:\Users\user\Documents\Adobe"
    a = Split(CreateObject("wscript.shell").Exec("cmd /c Dir """ & s & """ /s/b/a:d").StdOut.ReadAll, vbCrLf)
    If UBound(a) > 0 Then Cells(1, 1).Resize(UBound(a)) = Application.Transpose(a)
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,567
Messages
6,114,344
Members
448,570
Latest member
rik81h

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