This document provides examples of how to use a CountFilesInFolder macro to count files in a folder based on different criteria. The macro takes a folder path and optional file type parameter, and returns the number of files as a message box. Specific examples shown include counting TXT, Excel, and all files in a folder, as well as only files with "report" in the name.
This document provides examples of how to use a CountFilesInFolder macro to count files in a folder based on different criteria. The macro takes a folder path and optional file type parameter, and returns the number of files as a message box. Specific examples shown include counting TXT, Excel, and all files in a folder, as well as only files with "report" in the name.
This document provides examples of how to use a CountFilesInFolder macro to count files in a folder based on different criteria. The macro takes a folder path and optional file type parameter, and returns the number of files as a message box. Specific examples shown include counting TXT, Excel, and all files in a folder, as well as only files with "report" in the name.
To help get you started, here are a few examples of how to use the CountFilesInFolder macro:
Count txt files in Folder
Sub Demo() Call CountFilesInFolder("C:\Users\Ryan\Documents\", "*txt") End Sub
Count Excel files in Folder
Sub Demo2() Call CountFilesInFolder("C:\Users\Ryan\Documents\", "*.xls*") End Sub
Count All Files in Folder
Sub Demo3() Call CountFilesInFolder("C:\Users\Ryan\Documents\") End Sub
Only Count Files with "report" in the Filename
Sub Demo4() Call CountFilesInFolder("C:\Users\Ryan\Documents\", "*report*") End Sub Private Sub CountFilesInFolder(strDir As String, Optional strType As String) 'DEVELOPER: Ryan Wells (wellsr.com) 'DESCRIPTION: This macro counts the files in a folder and retuns the result in a msgbox 'INPUT: Pass the procedure a string with your directory path and an optional ' file extension with the * wildcard 'EXAMPLES: Call CountFilesInFolder("C:\Users\Ryan\") ' Call CountFilesInFolder("C:\Users\Ryan\", "*txt") Dim file As Variant, i As Integer If Right(strDir, 1) <> "\" Then strDir = strDir & "\" file = Dir(strDir & strType) While (file <> "") i = i + 1 file = Dir Wend MsgBox i End Sub