How to Copy Data from the Most Recently Modified Excel File Using VBA
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.

- 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.
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.
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.
Press ALT + F11 to launch the Visual Basic for Applications (VBA) editor. Insert a new module from the 'Insert' menu.
At the beginning of your macro, add the line: Application.ScreenUpdating = False to speed up the process and stop visual flickering.
Use the FileSystemObject or the Dir function to iterate through the target folder, tracking the DateLastModified property to locate the newest file path.
Open the identified file using the code: Workbooks.Open Filename:=latestFilePath, ReadOnly:=True.
Execute the copy operation directly. For example: Workbooks("Source.xlsm").Worksheets("Source").Range("B2:D50").Copy Destination:=Workbooks("Target.xlsm").Worksheets("Target").Range("E14").
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.

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. Open WPS Spreadsheet: Launch WPS Spreadsheet and navigate to the 'Developer' tab on the ribbon.
- 2. Access the Macro Editor: Click on 'Macros' or press ALT + F11 to open the built-in VBA editor.
- 3. Insert the Script: Paste your macro code for finding the newest file and copying the data range into a new module.
- 4. Run the Automation: Save your file as a Macro-Enabled Workbook (.xlsm) and run the macro to extract the data effortlessly.

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.




