July 2, 2017

How to edit multiple excel files in a folder automatically

How to edit multiple excel files in a folder automatically using VBA. A user wants to edit a range or ranges in multiple excel files or workbooks quickly. The first action is to access the folder and open each workbook one by one. Now each workbook may have multiple worksheets whose range has to be edited. So the next step is to count the total number of sheets in the excel workbook and loop through each of them and edit the range automatically. Once that is done we need to open the next workbook and perform the same action on all its sheets till all the workbooks are processed. Watch the training video below to see how the excel files are edited automatically and then you can study the VBA code:


Watch this video on YouTube.

Here’s the complete VBA code:

Sub LoopThroughDirectorytoEdit()
Dim MyFile As String
Dim Filepath As String
Dim q As Long

Filepath = “C:\TSS\”

MyFile = Dir(Filepath)

Do While Len(MyFile) > 0
If MyFile = “bookz.xlsm” Then
Exit Sub
End If

Workbooks.Open (Filepath & MyFile)
For q = 1 To Application.Worksheets.Count
Range(“B2”) = “7/5/2017”
Next q
MyFile = Dir
End Sub

Further Reading:

Do While Loop

2 thoughts on “How to edit multiple excel files in a folder automatically

  1. Thank you for your wonderful instruction of how to edit files in a folder.
    What if the excel files are many like 100, and they are all protected with the same password?
    How would I do that?
    Greetings from the sunshine island of Jamaica.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.