问题
I'd like to ask you how can I get the data from few determined (and always same) cells from many Excel .xls files, I.e. I have a list of .xls files in one folder, and each file has the same table inside, but with the different values. I would like to obtain the data from A1, C2, E3 from all files in folder and put them together into a new table in the new Excel file.
Is there a way how to do it, please? :) Thanks! ;)
回答1:
I retrieve external data as follows:
Create a worksheet called "x" that specifies the following info for each item of data I want to get:
So I have the folder name, the file name, the worksheet name, and the cell reference for the items in columns A, B, C, D
Then run the following macro:
Sub GetExternalData()
Dim wbPath As String, WorkbookName As String
Dim WorksheetName As String, CellRef As String
Dim Ret As String, i As Long, N As Long
For i = 1 To Sheets("x").Cells(Rows.Count, 1).End(xlUp).Row
wbPath = Sheets("x").Cells(i, 1).Value
WorkbookName = Sheets("x").Cells(i, 2).Value
WorksheetName = Sheets("x").Cells(i, 3).Value
CellRef = Sheets("x").Cells(i, 4).Value
Ret = "'" & wbPath & "[" & WorkbookName & "]" & _
WorksheetName & "'!" & Range(CellRef).Address(True, True, -4150)
Sheets("x").Cells(i, 5).Value = ExecuteExcel4Macro(Ret)
Next i
End Sub
The macro will fill column E with the data.
In your case column A will be filled with replicated values since your files are all in a single folder.
来源:https://stackoverflow.com/questions/25094694/how-to-obtain-the-data-from-cell-from-500-xls-excel-files