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

The 4thGen Intel Core i7 Processor

Intel has never ceased to innovate when it comes to its processors. Compare from the previous generation of Intel Core i7 processor has faster multiple applications along its amazing digital media creation. You can feel, better performance in every task you’ll do, especially with the Intel … [Read More...]

8 Great Budget-Friendly Ways for Growing Your YouTube Subscribers Count

If you are serious about creating independent video content, you probably have a YouTube account set up. And why not? YouTube is one of the most popular and budget-friendly video content-sharing social platforms in the digital space today. When created with public settings, YouTube content is … [Read More...]

Handheld Synchronization

Without the capability to transfer and synchronize data back to a desktop system, there's little benefit in having a word processor or similar feature on a PDA - particularly as relatively few devices support printing via a parallel printer port. Its no surpass then that this is a feature … [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

Testing the IP configuration – how to share a broadband Internet connection

After you've set up your IP configuration and restarted your PC you can use the winipcfg utility to confirm that things are working as intended as … [Read More...]

Why Drupal Accessibility is Vital for Your Website

Drupal may not be as popular as WordPress, but it is still used in over 1 million websites. The Internet might be more conducive to our needs if more … [Read More...]

Meeting the Agricultural Challenges of The Future with IoT

The Internet of Things is changing the future of many industries. One of the industries that will be most affected by the IoT is agriculture. It … [Read More...]

[footer_backtotop]

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