Forum Discussion
How to pull data from multiple workbooks
To pull data from the same cell across multiple workbooks/files that are labeled by date, you can use a combination of Excel functions and VBA to automate the process.
Here is a step-by-step guide to achieve this:
Step 1: Prepare Your Master Workbook
- Create a new sheet for the data extraction.
- List all the dates for the year in column A (e.g., A1 has the header "Date", and A2 to A366 list all dates from 01-Jan-2024 to 31-Dec-2024).
Step 2: Use VBA to Automate Data Extraction
You will need a VBA macro to loop through the dates, open the corresponding workbook, and extract the desired cell value.
- Press Alt + F11 to open the VBA editor.
- Insert a new module:
- Right-click on any existing module or the "VBAProject (YourWorkbookName)" > Insert > Module.
3. Paste the following VBA code into the module:
Vba Code is untested, please backup your file first.
Sub ExtractDataFromWorkbooks()
Dim ws As Worksheet
Dim dataSheet As Worksheet
Dim cellValue As Variant
Dim dateCell As Range
Dim filePath As String
Dim dateStr As String
Dim sourceCell As String
Dim i As Integer
' Set the worksheet variables
Set ws = ThisWorkbook.Sheets("Sheet1") ' Change to your sheet name
sourceCell = "B2" ' Change to the cell you want to extract data from
' Loop through each date in the list
For i = 2 To 366 ' Adjust based on the number of rows you have
dateStr = Format(ws.Cells(i, 1).Value, "yyyymmdd") ' Format the date to match your file naming convention
filePath = "C:\YourFolderPath\" & dateStr & ".xlsx" ' Change the folder path as needed
' Check if the file exists
If Dir(filePath) <> "" Then
' Open the workbook
Application.Workbooks.Open (filePath)
' Set the data sheet (assuming it's the first sheet in the workbook)
Set dataSheet = Workbooks(dateStr & ".xlsx").Sheets(1)
' Get the value from the specified cell
cellValue = dataSheet.Range(sourceCell).Value
' Close the workbook without saving
Workbooks(dateStr & ".xlsx").Close SaveChanges:=False
' Write the cell value to the master workbook
ws.Cells(i, 2).Value = cellValue
Else
' If the file doesn't exist, write an error message
ws.Cells(i, 2).Value = "File not found"
End If
Next i
End Sub
Step 3: Adjust the VBA Code to Your Needs
- Change "Sheet1" to the name of your sheet in the master workbook.
- Update sourceCell to the cell address from which you want to extract data (e.g., "B2").
- Set the filePath to the correct folder path where your daily files are stored (e.g., "C:\YourFolderPath\").
- Adjust the loop range if you have a different number of rows for dates (e.g., For i = 2 To 366 if you have a header in row 1).
Step 4: Run the Macro
- Press Alt + F8 to open the Macro dialog box.
- Select ExtractDataFromWorkbooks and click Run.
Tips for Future Use
- Keep the folder structure and naming convention consistent year after year.
- Ensure your daily files are named with a consistent format matching the date format in your master sheet.
By using this method, you can automate the extraction of data from multiple workbooks without manually opening each file, making it easier to maintain and use your template year after year.
Note: My knowledge of the topic is limited, but since no one has answered yet, even though it has been read many times, I posted the question in various AIs and found the above suggested solution for you. The proposed solution is untested.
My answers are voluntary and without guarantee!
Hope this will help you.
Was the answer useful? Mark as best response and Like it!
This will help all forum participants.