Macro to count the number of files in folder and subfolders

LisaLou

New Member
Joined
Mar 16, 2021
Messages
44
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am really hoping someone can help me with this. I have VBA code to count the numbers of files in folders but I also need to count the number of files in sub folders too which the total will be displayed in cells in the excel spreadsheet. This is the code I have for counting in folders:

Private Sub countBatches()

Dim FolderPath As String, path As String, count As Integer, dayid As String

dayid = Range("B1").Value

FolderPath = "C:\Users\Lisap\OneDrive\Desktop\Orders\" & dayid & "\Batches"

path = FolderPath & "\*"

Filename = Dir(path)

Do While Filename <> ""
count = count + 1
Filename = Dir()
Loop

Range("B2").Value = count

End Sub


Please can anyone help with this? I would be most grateful!
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hi & welcome to MrExcel.
How about
VBA Code:
Sub Lisalou()
   Dim fso As Object, StartFldr As Object
   Dim StartPth As String, DayId As String
   
   DayId = Range("B1").Value
   StartPth = "C:\Users\Lisap\OneDrive\Desktop\Orders\" & DayId & "\Batches"
   
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set StartFldr = fso.GetFolder(StartPth)
   Call RecursiveFolder(fso, StartFldr, True)
End Sub
Sub RecursiveFolder(fso As Object, Fldr As Object, IncludeSubFolders As Boolean)
   Dim FldrFile As Object
   Dim SubFldr As Object
   Dim NxtRw As Long
   
   NxtRw = Range("B" & Rows.Count).End(xlUp).Row + 1
   Range("A" & NxtRw) = Fldr.Name
   Range("B" & NxtRw) = Fldr.files.Count
   
   If IncludeSubFolders Then
      For Each SubFldr In Fldr.SubFolders
         Call RecursiveFolder(fso, SubFldr, True)
      Next SubFldr
   End If
End Sub
 
Upvote 0
Hi,

Thank you so much!

This code works great but how do I get it to put the total count of files in the sub folder to be displayed in cell B3?

I will be putting the date in cell B1 and then want the total count of files the sub folder displayed in B3?

Thanks!
 
Upvote 0
So you just want a total count of all files, rather than a count per folder?
 
Upvote 0
Yes, when I put the date in cell B1 I want a count of all the files in the sub folders which are in the file Batches to be displayed in cell B2. When I type a different date in cell B1 I want a count of all those files in the sub folder which are in the folder called Batches for this date.

hope this makes sense?
Thanks
 
Upvote 0
In that case use
VBA Code:
Sub Lisalou()
   Dim fso As Object, StartFldr As Object
   Dim StartPth As String, DayId As String
   
   DayId = Range("B1").Value
   StartPth = "C:\Users\Lisap\OneDrive\Desktop\Orders\" & DayId & "\Batches"
   
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set StartFldr = fso.GetFolder(StartPth)
   Call RecursiveFolder(fso, StartFldr, True)
End Sub
Sub RecursiveFolder(fso As Object, Fldr As Object, IncludeSubFolders As Boolean)
   Dim FldrFile As Object
   Dim SubFldr As Object
   Dim NxtRw As Long
   
   Range("B3") = Range("B3") + Fldr.files.Count
   
   If IncludeSubFolders Then
      For Each SubFldr In Fldr.SubFolders
         Call RecursiveFolder(fso, SubFldr, True)
      Next SubFldr
   End If
End Sub
 
Upvote 0
Hi,

Thank you so much for all the help but I can't get this to work, any ideas?

Thanks,
Lisa
 
Upvote 0
I run the macro but it doesn't show the number of files in cell B3 when I know there are files in the folder 'Batches' in the date I type in cell B1
Sub Lisalou()
Dim fso As Object, StartFldr As Object
Dim StartPth As String, DayId As String

DayId = Range("B1").Value
StartPth = "C:\Users\Lisap\OneDrive\Desktop\Orders\" & DayId & "\Batches"

Set fso = CreateObject("Scripting.FileSystemObject")
Set StartFldr = fso.GetFolder(StartPth)
Call RecursiveFolder(fso, StartFldr, True)
End Sub


The part in blue is highlighted when I try and run the macro
 
Upvote 0

Forum statistics

Threads
1,215,427
Messages
6,124,831
Members
449,190
Latest member
rscraig11

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