KEMBAR78
Macro Guide for File Counting | PDF
0% found this document useful (0 votes)
87 views2 pages

Macro Guide for File Counting

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.

Uploaded by

Yamini Shinde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views2 pages

Macro Guide for File Counting

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.

Uploaded by

Yamini Shinde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

How to use the CountFilesInFolder Macro

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

You might also like