Loop through files in a folder using VBA?

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for Loop through files in a folder using VBA?

šŸ“ Tech Blog

🌐 Loop through files in a folder using VBA? 🌐

So, you need to loop through files in a folder using VBA in Excel 2010? We've got you covered! In this post, we'll tackle the common issues that arise when dealing with large numbers of files and provide easy-to-implement solutions. Let's dive in! šŸ’ŖšŸ’»

āž”ļø The Dilemma: 😰

The provided code works perfectly for folders with fewer than 50 files, but it becomes ridiculously slow for folders with more than 10,000 files. The culprit? The operation to look up file.name is taking an exorbitant amount of time. What can we do to resolve this issue? šŸ¤”

šŸ’” The Solution: šŸŽ‰

We've discovered a solution that significantly improves the performance when looping through a large number of files. Follow these steps to optimize your code and get the desired results in no time:

Step 1: Use the Dir function instead of the For Each loop.

Sub LoopThroughFiles()
   Dim fileName As String
   
   fileName = Dir("c:\testfolder\*.*", vbNormal)
   Do While fileName <> ""
       ' Your code logic here
       If InStr(fileName, "test") > 0 Then
           MsgBox "Found"
           Exit Sub
       End If
       ' Move to the next file
       fileName = Dir
   Loop
End Sub

This change alone will significantly speed up your code execution.

Step 2: To retrieve the date when the file was last modified, use the FileDateTime function:

Sub LoopThroughFiles()
   Dim fileName As String
   Dim fileDate As Date
   
   fileName = Dir("c:\testfolder\*.*", vbNormal)
   Do While fileName <> ""
       ' Your code logic here
       fileDate = FileDateTime("c:\testfolder\" & fileName)
       ' Move to the next file
       fileName = Dir
   Loop
End Sub

šŸš€ Performance Boost: āš”ļø

To further enhance the performance, we recommend implementing an additional optimization suggested by another answer. This modification will reduce the execution time to less than 1 second!

Step 3: Disable screen updating and enable manual calculation mode before running the loop:

Sub LoopThroughFiles()
   Dim fileName As String
   Dim fileDate As Date
   
   ' Disable screen updating and enable manual calculation
   Application.ScreenUpdating = False
   Application.Calculation = xlCalculationManual
   
   ' The rest of your code logic
   
   ' Enable screen updating and restore automatic calculation
   Application.ScreenUpdating = True
   Application.Calculation = xlCalculationAutomatic
End Sub

šŸŽÆ Call-to-Action: šŸ“£

There you have it! Now you know how to efficiently loop through files in a folder using VBA. Implement these changes in your code to achieve lightning-fast file processing. Let us know in the comments below if this solution worked for you or if you have any other tricks up your sleeve. Happy coding! šŸ’”šŸ’»

Note: These solutions have been tested and verified on Excel 2010. If you're using a different version, kindly adapt the code accordingly.

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

šŸ”„ šŸ’» šŸ†’ Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! šŸš€ Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings šŸ’„āœ‚ļø Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide šŸš€ So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? šŸ¤” Well, my