VBA & Macro Problems

Import the Newest and Previous Weekly Files with an Excel VBA Macro

John WilsonJohn Wilson Nov 22, 2026 870 views

Question details

Import data from the newest weekly workbook and the previous weekly workbook into separate Excel worksheets.

Import the Newest and Previous Weekly Files with an Excel VBA Macro
Product
Microsoft Excel
Device & OS
not provided
Scenario
Weekly source files are named in sequence, such as S1, S2, and S3. The previous week's data should go to a worksheet named S-1.
Observed behavior
The desired macro should identify the two relevant files, copy their data to the correct destinations, and handle missing or empty files.
Before you start

Create representative source workbooks without confidential data and decide whether the weekly sequence number or the file date determines which files are newest. For the sample below, use files named S1.xlsx, S2.xlsx, and so on, and create empty destination sheets named Current and S-1.

Solution 1Recommended

Import the two highest-numbered weekly workbooks with VBA

Use this sample when the number in each filename determines the week and the data is on the first worksheet of each source workbook.

The sample copies values from each source sheet's used range, placing them at cell A1. It does not preserve formatting or formulas. It requires both destination sheets to be empty and both source sheets to contain data.

The folder path is a Windows example; change it to the actual folder path. If your filenames, source sheets, destination ranges, or definition of newest differ, adapt the macro before using it.

1
Prepare the workbook

In Excel, create empty worksheets named Current and S-1. Select File > Save As and save the destination workbook as an Excel Macro-Enabled Workbook (.xlsm).

2
Open the VBA editor

Press Alt+F11, select the destination workbook in the Project pane, and choose Insert > Module.

3
Add the sample macro

Paste the following code into the module and replace the folder path with your source folder: Sub ImportWeeklyFiles() Dim folder As String, f As String, digits As String Dim newest As String, previous As String Dim highest As Long, second As Long, n As Long Dim destNew As Worksheet, destOld As Worksheet Dim bookNew As Workbook, bookOld As Workbook Dim srcNew As Worksheet, srcOld As Worksheet Dim message As String On Error GoTo Failed folder = "C:\Weekly\" highest = -1: second = -1 Set destNew = ThisWorkbook.Worksheets("Current") Set destOld = ThisWorkbook.Worksheets("S-1") If Application.CountA(destNew.Cells) > 0 Or Application.CountA(destOld.Cells) > 0 Then Err.Raise vbObjectError + 1, , "Both destination sheets must be empty." End If f = Dir(folder & "S*.xlsx") Do While f <> "" digits = Mid$(f, 2, Len(f) - 6) If Len(digits) > 0 And Not (digits Like "*[!0-9]*") Then n = CLng(digits) If n > highest Then second = highest: previous = newest highest = n: newest = f ElseIf n < highest And n > second Then second = n: previous = f End If End If f = Dir() Loop If second < 0 Then Err.Raise vbObjectError + 2, , "Two distinct numbered weekly files are required." Set bookNew = Workbooks.Open(folder & newest, ReadOnly:=True) Set bookOld = Workbooks.Open(folder & previous, ReadOnly:=True) Set srcNew = bookNew.Worksheets(1) Set srcOld = bookOld.Worksheets(1) If Application.CountA(srcNew.Cells) = 0 Or Application.CountA(srcOld.Cells) = 0 Then Err.Raise vbObjectError + 3, , "A source worksheet is empty." End If With srcNew.UsedRange destNew.Range("A1").Resize(.Rows.Count, .Columns.Count).Value2 = .Value2 End With With srcOld.UsedRange destOld.Range("A1").Resize(.Rows.Count, .Columns.Count).Value2 = .Value2 End With bookNew.Close SaveChanges:=False bookOld.Close SaveChanges:=False MsgBox "Imported " & newest & " and " & previous Exit Sub Failed: message = Err.Description On Error Resume Next If Not bookNew Is Nothing Then bookNew.Close SaveChanges:=False If Not bookOld Is Nothing Then bookOld.Close SaveChanges:=False MsgBox "Import stopped: " & message End Sub

4
Run and verify the import

Close any open source workbooks, return to Excel, and select Developer > Macros. Choose ImportWeeklyFiles and select Run, then check Current and S-1 against the source files.

Import the two highest-numbered weekly workbooks with VBA
Adapt the sample to your files: If the import stops after copying one sheet, inspect both destination sheets before trying again. For a tailored macro, share a representative workbook and source-file examples with sensitive data removed, along with the source sheet names and destination ranges.
Free Microsoft Office alternative

Review Weekly Workbooks in WPS Office

WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and Microsoft Excel format compatibility for an easier transition. You can copy weekly data manually in WPS Spreadsheets; it does not fix or guarantee compatibility with an Excel VBA macro.

Open and review weekly spreadsheet data in WPS Spreadsheets.Compatible with Microsoft Excel formats such as .xlsx.Use a familiar interface for copying data between worksheets.Work with a free, lightweight Office alternative.
microsoft office alternative - wps office

Frequently Asked Questions

How does the macro decide which weekly file is newest?

The sample compares the numeric part of filenames such as S1.xlsx and S3.xlsx. The highest number is newest and the second-highest distinct number is previous; file modification dates are not used.

What happens if only one matching file is available?

The macro stops and displays a message that two distinct numbered weekly files are required. Add the missing file to the source folder, then run Developer > Macros > ImportWeeklyFiles again.

Can I import from a worksheet with a specific name?

Yes. In the VBA module, replace bookNew.Worksheets(1) and bookOld.Worksheets(1) with the appropriate worksheet names, such as Worksheets("Data"), in both source workbooks.

Will the macro copy formulas and formatting?

No. The sample assigns Value2 from each source used range, so it copies values rather than formulas or formatting. Modify the copy method if those elements are required.

Why does the macro require empty destination sheets?

The sample checks Current and S-1 before importing to avoid overwriting existing data. Create empty sheets or adapt the destination ranges and overwrite rules to your workbook.