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

LP to CD Connections

You can record from any analogue audio device that provides line-out terminals. Some analogue audio devices can be connected directly to your computer's sound card. However, the usual method for transferring material from vinyl LP is to connect the analogue audio device to a stereo pre-amp … [Read More...]

Understanding the Nuances and Nifty Features of Insurance Comparison Apps

Big data is changing many fields. Manufacturers are using smart technology to facilitate automation. However, insurer companies are benefiting more than most others. Alex Gayduk the Founder & CEO at Fortifier & Panzly, has talked extensively about the benefits of big data in the … [Read More...]

New Technology Trends for the New Year (Part 2)

We recently published an article on three major technology trends to expect in 2020. We wanted to share some more in this piece. Hyperautomation: the revolution of the machines We are talking about the implementation of Artificial Intelligence (AI) in automation, that is to say, that the … [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

Keep your Computer free from Viruses and More

Computer technology is advancing at a blistering pace, with each new year bringing stunning leaps and bounds over the previous year's already … [Read More...]

Win98 Installation Planning

A clean install means that you're going to completely wipe your C: drive early in the procedure. You need to be sure that you've figured out how … [Read More...]

The Ideal Password Length

The issue began on the password length, when there has been an announcement on the eBay administration that the fixed password they would be accepting … [Read More...]

[footer_backtotop]

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