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

Cyber Threat Hunting

What is Cyber Threat Hunting? Cyber Threat Hunting is the practice of proactively searching through computer networks for advanced threats and malicious factors that may have slipped past an SMEs traditional security endpoint. Fortunately, IT support companies like EC-MSP can provide new security … [Read More...]

Secure Your Connection to the Internet with VPN

With all the news of people being spied on while using the Internet, it is no wonder that more people are looking for ways to secure their online privacy. Problem is that people want a fast way to do this that does not interfere with the speeds they are accustomed to. A VPN connection may be the … [Read More...]

Installing new Windows XP graphics card drivers

Power up the PC and the Add New Hardware Wizard should be launched. Make sure you've no other programs running and click Next. If both your new graphics card and motherboard are Plug and Play compatible, Windows may detect your new card automatically. Click Next to see if it … [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

Samsung Galaxy Note 10.1, 4G LTE Tablet

  The Galaxy Note 10.1 (4G LTE Tablet) of Samsung is another successful mark of the company when it comes to mobile computers. This tablet … [Read More...]

CD-ROM Operations

Apart from far more sophisticated error-checking techniques, the innards of a CD-ROM drive are pretty much the same as … [Read More...]

Cyrix 6x86MX

Cyrix's response to Intel's MMX technology was the 6x86MX, launched in mid-1997, shortly before the company was acquired … [Read More...]

[footer_backtotop]

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