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!
 
Just noticed the image you added to post#56 & that looks as though you are looking in the wrong place, should you be looking in
C:\Users\Lisap\OneDrive\Desktop\Orders\120321\Complete
or
C:\Users\Lisap\Desktop\Orders\120321\Complete
I don't get where or what I am supposed to change. I don't understand why it would work for 110321 and not 120321.

Again, I appreciate your help.

Lisa
 
Upvote 0

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
The image you posted showing the folders looks to be looking at "C:\Users\Lisap\Desktop\Orders\120321\Complete" which is not where the code is looking.
Therefore where should the code be looking?
 
Upvote 0
The image you posted showing the folders looks to be looking at "C:\Users\Lisap\Desktop\Orders\120321\Complete" which is not where the code is looking.
Therefore where should the code be looking?
I need it to look in the orders folder and count what is in the folders and subfolders when I enter the date and then when I enter a new date it will refresh the counts and change them accordingly.
 
Upvote 0
Yes but where exactly are those folders?
 
Upvote 0
Yes but where exactly are those folders?
In the orders folder there are some 'Date' folders:

1616357991825.png


Each 'date' folder has the exact same folder structure:

1616358035601.png


In 'Batches' there are sub folders labelled: 001, 002, 003.... and there are files in these sub folders

The same is for the rest of the folders.... Complete will just have files. Queries has sub folders and files...

The code works great for 110321:

1616358150412.png


As the folder structure is exactly the same for each 'date' folder I should be able to type a different date such as 120321 and the code will loop through the folders, subfolders etc to give new counts.

I really hope I am making sense?
 
Upvote 0
Try using this & see if it helps
VBA Code:
         StartPth = "C:\Users\Lisap\Desktop\Orders\" & DayId & "\" & Cl.Value
 
Upvote 0
As I cannot see your computer or where the folders are located, there is not much more I can do.
You need to check that all the various paths are correct.
 
Upvote 0
As I cannot see your computer or where the folders are located, there is not much more I can do.
You need to check that all the various paths are correct.

Thank you so much for the help.
 
Upvote 0

Forum statistics

Threads
1,214,951
Messages
6,122,446
Members
449,083
Latest member
Ava19

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