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

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
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,223,237
Messages
6,170,928
Members
452,366
Latest member
TePunaBloke

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