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!
 
You cannot run the macro manually. If you add the message box as shown
VBA Code:
   Dim Cl As Range
   MsgBox "Ok"
   If Target.CountLarge > 1 Then Exit Sub
and change the value in B1 does the message box appear?
 
Upvote 0

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
I have added the above to the end of the Macro and this is all the code I have:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   Dim fso As Object, StartFldr As Object
   Dim StartPth As String, DayId As String
   Dim Cl As Range
   If Target.CountLarge > 1 Then Exit Sub
   If Target.Address(0, 0) = "B1" Then
      DayId = Range("B1").Value
      For Each Cl In Range("A2", Range("A" & Rows.Count).End(xlUp))
         StartPth = "C:\Users\Lisap\OneDrive\Desktop\Orders\" & DayId & "\" & Cl.Value
         Set fso = CreateObject("Scripting.FileSystemObject")
         Set StartFldr = fso.GetFolder(StartPth)
         Call RecursiveFolder(fso, StartFldr, Cl.Row, True)
      Next Cl
   End If
End Sub

Sub RecursiveFolder(fso As Object, Fldr As Object, Rw As Long, IncludeSubFolders As Boolean)
   Dim FldrFile As Object
   Dim SubFldr As Object
   Dim NxtRw As Long

   Range("B" & Rw) = Range("B" & Rw) + Fldr.Files.Count

   If IncludeSubFolders Then
      For Each SubFldr In Fldr.SubFolders
         Call RecursiveFolder(fso, SubFldr, Rw, True)
      Next SubFldr
   End If
End Sub

Dim Cl As Range
   MsgBox "Ok"
   If Target.CountLarge > 1 Then Exit Sub

I have changed the value in B1 from Date to Date MMDDYY press enter and nothing happens.
 
Upvote 0
You need to add the message box to the existing code where I showed.
 
Upvote 0
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   Dim fso As Object, StartFldr As Object
   Dim StartPth As String, DayId As String
   Dim Cl As Range
   MsgBox "Ok"
   If Target.CountLarge > 1 Then Exit Sub
   If Target.Address(0, 0) = "B1" Then
      DayId = Range("B1").Value
      For Each Cl In Range("A2", Range("A" & Rows.Count).End(xlUp))
         StartPth = "C:\Users\Lisap\OneDrive\Desktop\Orders\" & DayId & "\" & Cl.Value
         Set fso = CreateObject("Scripting.FileSystemObject")
         Set StartFldr = fso.GetFolder(StartPth)
         Call RecursiveFolder(fso, StartFldr, Cl.Row, True)
      Next Cl
   End If
End Sub

Sub RecursiveFolder(fso As Object, Fldr As Object, Rw As Long, IncludeSubFolders As Boolean)
   Dim FldrFile As Object
   Dim SubFldr As Object
   Dim NxtRw As Long

   Range("B" & Rw) = Range("B" & Rw) + Fldr.Files.Count

   If IncludeSubFolders Then
      For Each SubFldr In Fldr.SubFolders
         Call RecursiveFolder(fso, SubFldr, Rw, True)
      Next SubFldr
   End If
End Sub


I have added it where you said but still can't get it working. I don't know what I am doing wrong.
 
Upvote 0
Have you changed B1 after adding it?
 
Upvote 0
That sounds as though events have been disabled. Try closing Xl down completely, then open it up again & have another go.
 
Upvote 0
That sounds as though events have been disabled. Try closing Xl down completely, then open it up again & have another go.
Still doesn't work. Thanks for all your help I really do appreciate it.
 
Upvote 0
Are you physically editing B1 & then pressing enter or tab?
Also do you still have that code in the relevant sheet module?
 
Upvote 0
Are you physically editing B1 & then pressing enter or tab?
Also do you still have that code in the relevant sheet module?
I only have 1 module with this code:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   Dim fso As Object, StartFldr As Object
   Dim StartPth As String, DayId As String
   Dim Cl As Range
   MsgBox "Ok"
   If Target.CountLarge > 1 Then Exit Sub
   If Target.Address(0, 0) = "B1" Then
      DayId = Range("B1").Value
      For Each Cl In Range("A2", Range("A" & Rows.Count).End(xlUp))
         StartPth = "C:\Users\Lisap\OneDrive\Desktop\Orders\" & DayId & "\" & Cl.Value
         Set fso = CreateObject("Scripting.FileSystemObject")
         Set StartFldr = fso.GetFolder(StartPth)
         Call RecursiveFolder(fso, StartFldr, Cl.Row, True)
      Next Cl
   End If
End Sub

Sub RecursiveFolder(fso As Object, Fldr As Object, Rw As Long, IncludeSubFolders As Boolean)
   Dim FldrFile As Object
   Dim SubFldr As Object
   Dim NxtRw As Long

   Range("B" & Rw) = Range("B" & Rw) + Fldr.Files.Count

   If IncludeSubFolders Then
      For Each SubFldr In Fldr.SubFolders
         Call RecursiveFolder(fso, SubFldr, Rw, True)
      Next SubFldr
   End If
End Sub

Yes, once I enter the date in B1 I am pressing enter.
 
Upvote 0

Forum statistics

Threads
1,215,030
Messages
6,122,762
Members
449,095
Latest member
m_smith_solihull

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