VBA to check if folder has subfolder in it

  • Thread starter Thread starter AC
  • Start date Start date

AC

Board Regular
Joined
Mar 21, 2002
Messages
153
I found this code to check if a folder has files in it but I need to check if the folder has a sub folder in it, how can I do this in Excel 2003
Thanks


Code:
Dim fs, strFolderPath, oFolder

Set fs = CreateObject("Scripting.FileSystemObject")

strFolderPath = "d:\" ' this is your folder
Set oFolder = fs.GetFolder(strFolderPath)
If (oFolder.Files.Count = 0) Then
 'folder is Empty
  MsgBox "folder's empty"
Else
 ' folder isn't empty
  MsgBox "folder not empty. Files count: " & oFolder.Files.Count
End If

Set fs = Nothing
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Code:
[COLOR=darkblue]Dim[/COLOR] fs, strFolderPath, oFolder
    
[COLOR=darkblue]Set[/COLOR] fs = CreateObject("Scripting.FileSystemObject")
    
strFolderPath = "d:\" [COLOR=green]' this is your folder[/COLOR]
[COLOR=darkblue]Set[/COLOR] oFolder = fs.GetFolder(strFolderPath)
    [COLOR=darkblue]If[/COLOR] (oFolder.[B]SubFolders[/B].Count = 0) [COLOR=darkblue]Then[/COLOR]
     [COLOR=green]'folder is Empty[/COLOR]
      MsgBox "folder's empty"
    [COLOR=darkblue]Else[/COLOR]
     [COLOR=green]' folder isn't empty[/COLOR]
      MsgBox "folder not empty. [B]Subfolders [/B]count: " & oFolder.[B]SubFolders[/B].Count
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]

[COLOR=darkblue]Set[/COLOR] fs = [COLOR=darkblue]Nothing[/COLOR]
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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