Extracting the same cells from all Excel files in a folder

schillaci

New Member
Joined
Feb 9, 2022
Messages
6
Office Version
  1. 2019
I'm looking to extract the same cells from about 500 excel files that are contained in the same folder. Essentially I'm looking to pull out cell A5 which is the company name and cell A2 which is the date of the latest data in the file.
The final result should be like this with one line for each file in the folder;

Company name (cellA5)
date of last data (CellA2)
company ABC31/12/2022
Widget co30/9/2022
company XYZ30/9/2022
alpha company31/12/2022
delta co30/11/2022
123 ltd31/12/2022
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Run the macro, select the folder where the files are. The macro will take cells A5 and A2 from the files that are in the folder but always from the first sheet of each book.

The results will be on the first sheet of your book with the macro. If you want them in another sheet, change in this line With Sheets(1) (1) by the name of your sheet in quotes, for example With Sheets("Summary")

VBA Code:
Sub extract_same_cells()
  Dim sPath As String, sFile As String
  Dim i As Long
 
  With Application.FileDialog(msoFileDialogFolderPicker)
    .Title = "Select Folder"
    If .Show <> -1 Then Exit Sub
    sPath = .SelectedItems(1) & "\"
  End With
 
  sFile = Dir(sPath & "*.xls*")
  i = 2
  With Sheets(1)
    .Rows("2:" & Rows.Count).ClearContents
    Do While sFile <> ""
      .Range("A" & i).Value = GetObject(sPath & sFile).Sheets(1).Range("A5").Value
      .Range("B" & i).Value = GetObject(sPath & sFile).Sheets(1).Range("A2").Value
      i = i + 1
      sFile = Dir()
    Loop
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,650
Messages
6,126,019
Members
449,280
Latest member
Miahr

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top