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