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

Downsides of Using AI Art Cannot Be Overlooked

AI is changing the way designers and artists create their work. Artificial Intelligence makes it possible to create images faster, more accurately and efficiently. This revolutionary technology delivers results with complex patterns and nuances that would otherwise be impossible to recreate … [Read More...]

Top 4 Free Online Design Tools that Use Machine Learning

A few months ago, we told you about a series of free online image editing tools. In practice, these are simple alternatives to Adobe Photoshop that are often used for common actions such as cropping an image, placing a frame or adding brightness to a photograph. However, all these tools, including … [Read More...]

Website Usage and Disclaimer – pctechguide.com

The purpose of the PC Technology Guide web site is to provide a free information resource for the benefit of the Internet community at large. To this end the site's content is licensed to the public under the GNU Free Documentation License (GFDL). This effectively allows PCTechGuide content to … [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

Kill Running Process on Computer

Method #1: There are many ways to kill an active process, and in this article we will examine some of the most common and effective approaches to … [Read More...]

Nvidia Machine Learning Leads to Impressive 3D Graphics

New machine learning technology is constantly changing the future of graphic design. Nvidia is creating some impressive changes in this area. Their … [Read More...]

DiskImage 5 Review

PROS: DiskImage comes with uncomplicated menus and options. It allows for a full image data backup of your computer. CONS: There are no controls to … [Read More...]

[footer_backtotop]

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