Automation · VBA

Loop Through Files in a Folder

Open every Excel file in a folder and process it.

download.bas (ES) download.bas (EN)
' Loops through every .xlsx in a chosen folder and prints the file name + row count.
Sub LoopFilesInFolder()
    Dim folderPath As String, file As String
    Dim wb As Workbook, n As Long
    With Application.FileDialog(msoFileDialogFolderPicker)
        If .Show <> -1 Then Exit Sub
        folderPath = .SelectedItems(1) & "\"
    End With

    file = Dir(folderPath & "*.xlsx")
    Do While file <> ""
        Set wb = Workbooks.Open(folderPath & file, ReadOnly:=True)
        Debug.Print file, wb.Sheets(1).UsedRange.Rows.Count
        wb.Close SaveChanges:=False
        n = n + 1
        file = Dir
    Loop
    MsgBox "Processed " & n & " files. See the Immediate window (Ctrl+G).", vbInformation
End Sub

Free to use and modify · ExcelBot — excelempowers.com

Lets you pick a folder, then opens each .xlsx in turn (read-only) and logs its row count — a base to automate batch tasks.

How to install:

  1. Open your workbook and press Alt + F11 to open the VBA editor.
  2. Choose Insert ▸ Module and paste the code (or File ▸ Import File for the .bas).
  3. Press F5 to run, or run it from Developer ▸ Macros.
  4. Save your file as .xlsm (macro-enabled) to keep the macro.

No VBA? In ExcelBot you can do the same thing by just asking the AI in plain language.

loop files folder batch automation recorrer lotes
Rather not use VBA?

In ExcelBot you get the same result by asking the AI in plain language — no macros needed.

Try ExcelBot free Browse more macros

Related macros