Excel – VBA – Loop through all workbooks in a Folder

SCENARIOI want to loop through all [gs workbook]s in a folder and perform an operation on each of them.

SOLUTION:

Sub AllFolderFiles() 

Dim wb As Workbook
Dim TheFile As String
Dim MyPath As String MyPath = "C:\Temp"
ChDir MyPath
TheFile = Dir("*.xls")
Do While TheFile <> ""
Set wb = Workbooks.Open(MyPath & "\" & TheFile)
MsgBox wb.FullName
wb.Close
TheFile = Dir
Loop
End Sub

NOTE: If you want to loop through .xlsx workbooks, you'll need to only change the TheFile = Dir("*.xls) to TheFile = Dir("*.xlsx).

You'll need to update even the MyPath [gs variable] to let the code to search for your [gs workbook]s.

SOURCE

LINK (Contextures.com)

LANGUAGE
ENGLISH

1 thought on “Excel – VBA – Loop through all workbooks in a Folder”

Comments are closed.