VBA Count Number of Files in Folder and Subfolders. We are checking main folder and all the other available sub-folders to count number of files. We use recursive method to check each and every sub folder. In the following tutorial we have explain step by step instructions to run example macro.
Let us see an example macro VBA code to count number of files in folder and Sub-folders. In the below example we are specifying root folder path as ‘C:\VBAF1’. You can change this path according to your requirement.
'Module level variable Dim iFilesCount As Integer 'VBA Procedure to Count Files in Folder and Subfolders Sub VBAF1_Count_Files_in_Folder_and_Subfolders() 'Variable declaration Dim sFldPath As String 'Define Root Folder Path sFldPath = "C:\VBAF1" 'Çall SubProcedure Call VBAF1_SubProcedure_To_Count_Files_in_Folder_and_Subfolders(sFldPath) MsgBox "Number of files in Folder and Subfolders : " & iFilesCount, vbInformation, "VBAF1" End Sub
Note: We have declared module level variable ‘iFilesCount’. It helps in recursive process. The variable declared at the beginning of the module. Read more details about Module level variable.
SubProcedure(Recursive Method): In the below Sub-procedure we are checking all available subfolders using recursive method. We are checking files count and sub-folders count in a folder. We use VBA ‘FolderExists’ method of FileSystemObject object(FSO) to check whether specified folder is available or not.
'VBA SubProcedure(Recursive Method) to Count Files in Folder and Subfolders Sub VBAF1_SubProcedure_To_Count_Files_in_Folder_and_Subfolders(sFolderPath As String) 'Variable declaration Dim sFileName As String, oFile As Object, oFolder As Folder Dim iFoldersCount As Integer, iFilesCnt As Integer Dim iAddFilesCount As Integer 'Check for slash If Right(sFolderPath, 1) <> "\" Then sFolderPath = sFolderPath & "\" 'Create FSO Object Set oFSO = CreateObject("Scripting.FileSystemObject") Set oFolder = oFSO.GetFolder(sFolderPath) 'Check Specified Folder exists or not If oFSO.FolderExists(sFolderPath) Then iFilesCnt = oFolder.Files.Count iFilesCount = iFilesCount + iFilesCnt End If 'Loop through all subfolders For Each oSubFolder In oFolder.SubFolders 'Çheck available files count in a folder iAddFilesCount = oSubFolder.Files.Count 'Çheck available subfolders count in a folder iFoldersCount = oSubFolder.SubFolders.Count 'Recursive Method 'Check if any other subfolders within subfolders If iFoldersCount <> 0 Then Call VBAF1_SubProcedure_To_Count_Files_in_Folder_and_Subfolders(sFolderPath & oSubFolder.Name) Else 'Add files count iFilesCount = iFilesCount + iAddFilesCount End If Next End Sub
Output: You can find following output screenshot for your reference. It displays total number of files from a folder and Sub-folders. Now you can find number of files count in a specified folder and folders on the screen.
You can refer the following link for the step by step instructions.
Click on the following links of the useful resources. These helps to learn and gain more knowledge.