With this macro, sub-folders are listed for first and second levels (based on selected folder). The output is divided into separate columns in this example.
I now want it to list folders only if any of the sub-folders contain the string "OK". The sub-folder with this string can be at any level.
EXAMPLE:
Selected folder
..... Subfolder 1-A
.......... Subfolder 2-A
.................... Subfolder 3-A OK
......................... Subfolder 4-A
......................... Subfolder 4-B
.................... Subfolder 3-B
......................... Subfolder 4-C
.......... Subfolder 2-B
.................... Subfolder 3-C
..... Subfolder 1-B
.......... Subfolder 2-C
.................... Subfolder 3-D
......................... Subfolder 4-D
......................... Subfolder 4-E
.................... Subfolder 3-E
.......... Subfolder 2-D
.......... Subfolder 2-E
.................... Subfolder 3-F
......................... Subfolder 4-F
.................... Subfolder 3-G
......................... Subfolder 4-G
......................... Subfolder 4-H
.................... Subfolder 3-H
.................... Subfolder 3-I
......................... Subfolder 4-I
..... Subfolder 1-C
.......... Subfolder 2-F
.......... Subfolder 2-G
.......... Subfolder 2-H
.................... Subfolder 3-J
......................... Subfolder 4-J OK
I now want it to list folders only if any of the sub-folders contain the string "OK". The sub-folder with this string can be at any level.
EXAMPLE:
Selected folder
..... Subfolder 1-A
.......... Subfolder 2-A
.................... Subfolder 3-A OK
......................... Subfolder 4-A
......................... Subfolder 4-B
.................... Subfolder 3-B
......................... Subfolder 4-C
.......... Subfolder 2-B
.................... Subfolder 3-C
..... Subfolder 1-B
.......... Subfolder 2-C
.................... Subfolder 3-D
......................... Subfolder 4-D
......................... Subfolder 4-E
.................... Subfolder 3-E
.......... Subfolder 2-D
.......... Subfolder 2-E
.................... Subfolder 3-F
......................... Subfolder 4-F
.................... Subfolder 3-G
......................... Subfolder 4-G
......................... Subfolder 4-H
.................... Subfolder 3-H
.................... Subfolder 3-I
......................... Subfolder 4-I
..... Subfolder 1-C
.......... Subfolder 2-F
.......... Subfolder 2-G
.......... Subfolder 2-H
.................... Subfolder 3-J
......................... Subfolder 4-J OK
Code:
CURRENT OUTPUT
--------------
Subfolder 1-A Subfolder 1-B Subfolder 1-C
Subfolder 2-A Subfolder 2-C Subfolder 2-F
Subfolder 2-B Subfolder 2-D Subfolder 2-G
Subfolder 2-E Subfolder 2-H
Code:
DESIRED OUTPUT
--------------
Subfolder 1-A Subfolder 1-C
Subfolder 2-A Subfolder 2-H
Code:
Sub getfolders()
Dim fs, f, fc, fcsub
Dim n As String
Dim r As Integer, c As Integer
' Open the Browse Folder dialog
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
.InitialFileName = "C:\"
If .Show = 0 Then 'User pressed Cancel
Exit Sub
End If
folderspec = .SelectedItems(1)
End With
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(folderspec)
Set fc = f.subfolders
n = f.Name
r = 1
c = 1
For Each f1 In fc
Sheets(1).Cells(r, c) = f1.Name
Set fcsub = f1.subfolders
r = r + 1
For Each f2 In fcsub
Sheets(1).Cells(r, c) = f2.Name
r = r + 1
Next
c = c + 1
r = 1
Next
End Sub