pctechguide.com

  • Home
  • Guides
  • Tutorials
  • Articles
  • Reviews
  • Glossary
  • Contact

VBA Macro that Counts the Number of Files in a Folder

You may want to count the number of PNG files in a folder. This is pretty easy if you only have one folder. You just have to right-click the folder and then click the “Properties” attribute. However, this can be a pain if you have a lot of folders, because you need to do this process for every one of them.

Fortunately, you can solve this process pretty easily if you have a VBA macro that does this job. We created one that can this. It has a loop, so you can copy and paste a number of folder names in the first column and then it will show the results for every folder in the second column.

VBA Macro to Count Files in Folders

Sub CountFilesInFolders()
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim iRow As Integer
Dim folderPath As String
Dim fileCount As Integer

' Create FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Loop through each row in column A until the first empty cell is encountered
iRow = 1
Do Until IsEmpty(Cells(iRow, 1).Value)
    ' Get folder path from column A
    folderPath = Cells(iRow, 1).Value

    ' Check if folder exists
    If objFSO.FolderExists(folderPath) Then
        ' Set objFolder to the folder
        Set objFolder = objFSO.GetFolder(folderPath)

        ' Initialize file count
        fileCount = 0

        ' Loop through each file in the folder and count
        For Each objFile In objFolder.Files
            fileCount = fileCount + 1
        Next objFile

        ' Output file count to column B
        Cells(iRow, 2).Value = fileCount
    Else
        ' If folder doesn't exist, output error message
        Cells(iRow, 2).Value = "Folder doesn't exist"
    End If

    ' Move to the next row
    iRow = iRow + 1
Loop

' Release objects
Set objFile = Nothing
Set objFolder = Nothing
Set objFSO = Nothing

End Sub

How Do You Run this Program?

It is pretty easy to run this macro. You just have to follow these steps:

  1. Open Excel and make sure that you have a file that is enabled to handle macros.
  2. Press Alt + F11 to open the VBA Editor.
  3. Insert a new module by clicking Insert and then click the Module option.
  4. Copy and paste the code that we showed above into the module window.
  5. Close the VBA Editor.
  6. Copy and paste all of your folder paths in the first column (i.e. Column A)
  7. Run the macro CountFilesInFolders: Developer > Macros > CountFilesInFolders > Run.

You could also run the macro by clicking the “Run” button in the VBA Editor. It will loop through all of the folder paths in the first column and share the number of files in the second column.

Filed Under: Articles

Latest Articles

How to Turn Off Firewall in Windows 7

There are times where you may want to turn off the firewall in Windows 7, or at least disable it temporarily. This typically is necessary when a program will not work correctly. The firewall should be on your troubleshooting list of things to check when programs are not behaving correctly. This … [Read More...]

Motherboard IDE Connections

IDE connectors, like floppy drive and other connectors, are keyed to ensure proper connection. Connect your IDE devices to the motherboard IDE connectors, taking care to assign these to give optimal performance for your particular system configuration. Although a single IDE channel can … [Read More...]

Intel 440 Chipsets – 440LX, 440EX, 440BX, 440ZX, 440GX

The Intel 440 series of chipsets built on the Pentium's success to advance Intel dominance. 440LX The 440LX (by this time Intel had dropped the term Triton) was the successor to the Pentium Pro 440FX chipset and was developed by Intel to consolidate on the critical success of the Pentium II … [Read More...]

Gaming Laptop Security Guide: Protecting Your High-End Hardware Investment in 2025

Since Jacob took over PC Tech Guide, we’ve looked at how tech intersects with personal well-being and digital safety. Gaming laptops are now … [Read More...]

20 Cool Creative Commons Photographs About the Future of AI

AI technology is starting to have a huge impact on our lives. The market value for AI is estimated to have been worth $279.22 billion in 2024 and it … [Read More...]

13 Impressive Stats on the Future of AI

AI technology is starting to become much more important in our everyday lives. Many businesses are using it as well. While he has created a lot of … [Read More...]

Graphic Designers on Reddit Share their Views of AI

There are clearly a lot of positive things about AI. However, it is not a good thing for everyone. One of the things that many people are worried … [Read More...]

Redditors Talk About the Impact of AI on Freelance Writers

AI technology has had a huge impact on our lives. A 2023 survey by Pew Research found that 56% of people use AI at least once a day or once a week. … [Read More...]

11 Most Popular Books on Perl Programming

Perl is not the most popular programming language. It has only one million users, compared to 12 million that use Python. However, it has a lot of … [Read More...]

Guides

  • Computer Communications
  • Mobile Computing
  • PC Components
  • PC Data Storage
  • PC Input-Output
  • PC Multimedia
  • Processors (CPUs)

Recent Posts

Digital Broadcasting

In Europe the Digital Video Broadcasting (DVB) project was set up in 1993 and came from a market led perception that … [Read More...]

Belarc Advisor – Help Keep Your System Secure and Up To Date

The Belarc Advisor tool is a useful tool for keeping your system secure and up to date. It also can tell you a good deal of information about system … [Read More...]

CDR-RW Double Density Media

The idea of a double density writable and rewritable CD media is not new. In the early 1990s a number of companies … [Read More...]

[footer_backtotop]

Copyright © 2025 About | Privacy | Contact Information | Wrtie For Us | Disclaimer | Copyright License | Authors