Excel VBA: Open the Latest Downloaded File Automatically

VBA & macros · Updated November 19, 2026

Yes. In Excel for Microsoft 365 on Windows, a VBA macro can scan your Downloads folder, identify the newest supported Excel file by its creation time, and open it. The example below checks the folder itself—not its subfolders—and lets you limit the search to workbook and CSV files.

What “latest downloaded file” means

Excel does not have a built-in command that knows which file you most recently downloaded. A macro needs a folder to search and a rule for deciding which file is newest. The example uses the file’s creation timestamp on your computer. For downloads saved directly into the same folder, that is often a useful approximation of download order.

That definition has limits. A file’s creation time is not necessarily the time its download finished. A browser can create a file while a download is still in progress, and copying or moving a file can change its creation time. Also, the file’s modified time may come from information supplied by its source rather than from the moment you downloaded it. The macro therefore finds the newest eligible file by local creation time; it cannot establish which download completed most recently in every situation.

The code accepts Excel workbooks and CSV files. It ignores other file types, including common temporary download extensions, and excludes the workbook containing the macro so it will not simply reopen itself.

VBA macro to find and open the newest file

Paste this procedure into a standard VBA module in an Excel workbook saved on your Windows computer. It uses late binding for FileSystemObject, so you do not need to add a reference in the VBA editor.

Option Explicit

Sub OpenLatestDownloadedFile()
    Dim fso As Object
    Dim downloads As Object
    Dim item As Object
    Dim downloadsPath As String
    Dim bestPath As String
    Dim extension As String
    Dim itemCreated As Date
    Dim bestCreated As Date
    Dim itemModified As Date
    Dim bestModified As Date

    ' Change this path if your Downloads folder is elsewhere.
    downloadsPath = Environ&$("USERPROFILE") & "\Downloads"

    Set fso = CreateObject("Scripting.FileSystemObject")
    If Not fso.FolderExists(downloadsPath) Then
        MsgBox "Downloads folder not found: " & downloadsPath, vbExclamation
        Exit Sub
    End If

    Set downloads = fso.GetFolder(downloadsPath)

    For Each item In downloads.Files
        extension = LCase$(fso.GetExtensionName(item.Name))

        Select Case extension
            Case "xlsx", "xlsm", "xlsb", "xls", "csv"
                ' Do not select the workbook that contains this macro.
                If StrComp(item.Path, ThisWorkbook.FullName, vbTextCompare) <> 0 Then
                    itemCreated = item.DateCreated
                    itemModified = item.DateLastModified

                    ' Use modified time only to break a creation-time tie.
                    If Len(bestPath) = 0 Or itemCreated > bestCreated _
                       Or (itemCreated = bestCreated And itemModified > bestModified) Then
                        bestPath = item.Path
                        bestCreated = itemCreated
                        bestModified = itemModified
                    End If
                End If
        End Select
    Next item

    If Len(bestPath) = 0 Then
        MsgBox "No eligible Excel or CSV file was found in Downloads.", vbInformation
        Exit Sub
    End If

    Workbooks.Open Filename:=bestPath
End Sub

The line that builds the folder path uses Windows’ USERPROFILE environment variable, so a typical path is C:\Users\YourName\Downloads. The code checks only files directly inside that folder. If the folder does not exist at that path, it displays a message rather than attempting to open a file.

The comparison selects the file with the latest creation time. If two files have exactly the same creation time, it uses the later modified time as a tie-breaker. If both timestamps also match, the first matching file encountered is selected, so the outcome in that rare case is not a meaningful ranking.

Put the macro in a workbook and run it

Save a copy of your workbook before running any macro. VBA changes cannot be reversed with Ctrl+Z; this particular procedure opens a file but does not edit its contents. Save the macro workbook as an Excel Macro-Enabled Workbook (.xlsm), because the .xlsx format does not retain VBA code.

  1. In Excel for Windows, open the workbook that will hold the macro. If the Developer tab is missing, choose File > Options > Customize Ribbon, select Developer in the main tabs list, and choose OK.
  2. Choose Developer > Visual Basic, or press Alt+F11 to open the Visual Basic Editor.
  3. In the editor, choose Insert > Module and paste the procedure into the new module.
  4. Save the workbook as an .xlsm file. Return to Excel and choose Developer > Macros, select OpenLatestDownloadedFile, and choose Run.

Alt+F11 is the Windows shortcut to open the VBA editor. On a Mac, Option+F11 is commonly used; depending on the keyboard, you may also need Fn. The code above is Windows-specific because it constructs a Windows Downloads path and uses the Windows Scripting.FileSystemObject. Excel for the web cannot run VBA macros. On a Mac, use a Mac-specific folder path and an approach supported by that Excel installation rather than assuming this Windows procedure will work unchanged.

Change the folder and the file types

If your browser saves downloads to another location, edit the downloadsPath assignment near the top of the procedure. For example, replace the path-building expression with a full path in quotes. Use the actual folder path on your computer; a company may redirect Downloads to a network location or a OneDrive-synced folder. The standard path is not guaranteed for every Windows account.

The Select Case block defines eligible extensions. To include another format Excel can open, add its extension in lowercase and in quotation marks, separated by commas—for example, “txt” for a text file. Whether the file opens as a worksheet, a delimited text file, or another kind of workbook depends on its format and Excel’s import behavior. Do not add an extension simply because a file has it: the macro does not inspect the contents to verify that the file is a valid workbook.

To search a different folder, change the path rather than adding broad searches across the whole computer. Searching a known download location is faster, makes the selection easier to predict, and reduces the chance of opening an unrelated workbook with a recent timestamp.

Creation time, modified time, and download order

Creation time is a practical choice when the goal is “the file that appeared in this folder most recently.” Modified time answers a different question: when Windows reports that the file contents were last changed. A downloaded document may retain a source-provided modified timestamp, so sorting solely by modified time can select an older download. Conversely, a local edit to an older download can make its modified time newer than that of a file downloaded later.

Choose the timestamp that matches your workflow:

  • Creation time: Useful for identifying a file newly created in the Downloads folder, but moving or copying files can affect the result.
  • Modified time: Useful when the newest edit matters, but not always a reliable record of download order.
  • A chosen filename or date in the name: More predictable when a source uses a stable naming convention, but requires a different selection rule.

The macro does not monitor the browser or wait for a download to finish. Run it after the download has completed. If several downloads are in progress together, a file created first might finish last; the macro still chooses by creation timestamp. For a workflow that must act on completion, first save downloads into a staging folder and run the macro after confirming the desired file is complete.

What happens when the workbook opens

The final statement calls Workbooks.Open with the selected full path. Excel opens the file as another workbook; it does not replace the workbook containing the macro. If the selected file is a CSV, Excel opens it as a workbook containing the imported text data. Regional list separators and the CSV’s encoding can affect how text is divided or displayed.

Excel may display a prompt or apply security protections depending on the file, its origin, and your organization’s settings. A downloaded macro-enabled workbook can contain VBA of its own. Only enable its macros when you trust the source and understand why they are needed. This macro does not bypass Excel’s security controls, and it should not be modified to disable Protected View or macro warnings.

Common problems and practical fixes

  • It says no eligible file was found: Confirm the folder path and check that the file ends in .xlsx, .xlsm, .xlsb, .xls, or .csv. Files in subfolders are not included.
  • It opens the wrong file: Check the creation timestamps of files in Downloads. Remove unwanted extensions from the list, or use a more specific filename rule if the folder contains unrelated files.
  • The folder path is wrong: Downloads may have been redirected. In File Explorer, open the actual download location and update downloadsPath in the macro.
  • The macro will not run: Make sure the containing workbook is saved as .xlsm and that your Excel security settings permit the macro. Do not lower security settings broadly just to run code from an untrusted source.
  • It selects a file that is still downloading: Wait for the browser’s download to complete before running the macro. Temporary extensions are excluded, but the procedure is not a download-completion monitor.

If a file is already open, Excel may activate its existing window rather than create a second independent copy. If opening a file produces an error, check whether it is incomplete, password-protected, damaged, or blocked by a policy before changing the macro.

Safer alternatives for repeatable workflows

For occasional use, opening Downloads in File Explorer and sorting by Date created or Date modified is a simple way to inspect candidates before opening one. That extra check is valuable when the folder contains sensitive files or when opening an unexpected workbook could run untrusted code.

If the same named file is downloaded repeatedly, consider a more controlled workflow: save it to a dedicated folder, use a consistent filename, and have the macro open that exact path. This avoids guessing which of several files is newest. If you need to process many files rather than open one, Power Query or a purpose-built import procedure may be more appropriate than choosing a single latest file.

The appropriate approach depends on what “latest” means in your process. For a casual Downloads-folder shortcut, a creation-time scan is convenient. For an auditable or business-critical process, explicit filenames, a designated input folder, and a check that the download is complete provide clearer controls than relying on timestamps alone.

Frequently Asked Questions

Can VBA open the most recently downloaded file automatically?
Yes. A macro can scan a specified folder, compare file timestamps, and call Workbooks.Open on the selected file. The example uses creation time and must be run after the download is complete.

Does this macro search Downloads subfolders?
No. It examines files directly in the specified Downloads folder only. A recursive search would need extra code to visit each subfolder.

Why does it open a different file than the one I just downloaded?
The macro ranks eligible files by local creation time, not browser history or download completion time. A redirected folder, a moved file, or multiple simultaneous downloads can affect which file ranks newest.

Can I use this macro in Excel for the web?
No. Excel for the web does not run VBA macros. Use Excel desktop on Windows for the code as written.

Can I use it on a Mac?
Not unchanged. The path expression and FileSystemObject approach are Windows-specific. A Mac version needs a Mac-appropriate folder path and filesystem method, and should be adapted and checked on that computer.

Will it open a CSV file?
Yes, because .csv is included in the extension list. Excel opens it as a workbook, though the way columns and characters appear can depend on the CSV’s format and regional settings.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.