VBA & Macro Problems

How to Copy Data from the Most Recently Modified Excel File Using VBA

Camila MilosovichCamila Milosovich Oct 26, 2026 869 views

Question details

The user wants to use an Excel VBA macro to identify the newest or most recently modified workbook in a directory, extract specific data from it, and copy that data into a target workbook.

How to Copy Data from the Most Recently Modified Excel File Using VBA
Product
Microsoft Excel / WPS Spreadsheet
Device & OS
not provided
Scenario
Automating data consolidation by pulling information from dynamically updated files in a specific folder without manually searching for the newest file.
Observed behavior
The macro needs to silently open the latest file, directly copy the specified range to the destination without visual interruption, and close the source file.
Before you start

Ensure you have the exact file path of the directory containing the source files, and verify that Developer options and macros are enabled in your spreadsheet software.

Solution 1Recommended

Use VBA to Find and Extract Data from the Latest Workbook

Write a VBA script to disable screen updating, identify the most recently modified file, open it as read-only, and copy the required range directly into the target workbook.

Disabling screen updating makes the macro run much faster and prevents the screen from flickering while files are processed in the background. Additionally, opening the source file as read-only ensures that the original data is protected from accidental modifications.

To maximize efficiency, directly map the source range to the destination range rather than selecting cells individually.

1
Open the VBA Editor

Press ALT + F11 to launch the Visual Basic for Applications (VBA) editor. Insert a new module from the 'Insert' menu.

2
Disable Screen Updating

At the beginning of your macro, add the line: Application.ScreenUpdating = False to speed up the process and stop visual flickering.

3
Identify the Most Recently Modified File

Use the FileSystemObject or the Dir function to iterate through the target folder, tracking the DateLastModified property to locate the newest file path.

4
Open Source Workbook as Read-Only

Open the identified file using the code: Workbooks.Open Filename:=latestFilePath, ReadOnly:=True.

5
Copy the Specified Range

Execute the copy operation directly. For example: Workbooks("Source.xlsm").Worksheets("Source").Range("B2:D50").Copy Destination:=Workbooks("Target.xlsm").Worksheets("Target").Range("E14").

6
Close the Source and Restore Settings

Close the source workbook without saving changes using ActiveWorkbook.Close SaveChanges:=False, and restore the display with Application.ScreenUpdating = True at the end of your script.

Use VBA to Find and Extract Data from the Latest Workbook
Optimize Folder Lookups: Avoid repeating folder lookups like unnecessary GetFolder statements in your code. Once the most recent file is identified, proceed straight to opening and copying to ensure the macro remains fast.
Automate with WPS Spreadsheet

Run VBA Macros Seamlessly in WPS Spreadsheet

WPS Office offers native support for VBA and macros, allowing you to run your automated data extraction scripts just like you would in Microsoft Excel. You can quickly identify recently modified files and consolidate data efficiently in WPS Spreadsheet.

  1. 1. Open WPS Spreadsheet: Launch WPS Spreadsheet and navigate to the 'Developer' tab on the ribbon.
  2. 2. Access the Macro Editor: Click on 'Macros' or press ALT + F11 to open the built-in VBA editor.
  3. 3. Insert the Script: Paste your macro code for finding the newest file and copying the data range into a new module.
  4. 4. Run the Automation: Save your file as a Macro-Enabled Workbook (.xlsm) and run the macro to extract the data effortlessly.
Full support for advanced VBA macros and scriptingSeamless compatibility with Microsoft Office .xls, .xlsx, and .xlsm formatsLightweight application that loads fast and processes data quicklyCompletely free core spreadsheet functionalities with an intuitive interface
microsoft office alternative - wps office

Frequently Asked Questions

How do I find the most recently modified file in a folder using VBA?

You can use the 'Scripting.FileSystemObject'. Iterate through all files in a specific folder, compare their 'DateLastModified' properties, and store the file path of the one with the most recent date into a string variable.

Why should I disable screen updating in my Excel macro?

Setting 'Application.ScreenUpdating = False' stops Excel from refreshing the screen every time a file opens or data pastes. This significantly speeds up the macro's execution time and prevents the screen from flashing uncontrollably.

How can I copy data in VBA without using the Select or Activate methods?

You can copy directly by linking the source range and destination range on a single line. Use the syntax: 'SourceRange.Copy Destination:=TargetRange'. This approach is not only faster but also much less prone to errors than selecting individual cells.