简体   繁体   中英

Copying data from a list of multiple standardised workbooks to the active workbook

I'd like to for each object in a table, open the workbook from a file path in column 4 and copy about 52 cells (which will be in the same place in each workbook) into my active spreadsheet.

Table looks like this

Code is rudimentary because I thought if I could solve the first copy, I could replicate it further

        Set tbl = Sheet1.ListObjects("OTJ")  
        For Each cell In tbl.DataBodyRange.Columns(4).Cells
        WB = cell.Value
        Workbooks.Open Filename:=WB
    Set x = Workbooks.Open(WB)
    Set y = ActiveWorkbook
    v = x.Sheets("Sheet2").cell("D70")
Cells(2, 5) = v
    x.Close

I keep getting the subscript out of range error, please help

It's not really clear where you want to put the extracted data, but something along these lines should work:

Dim tbl As ListObject, cell As Range, wb As Workbook
Dim wsData As Worksheet

Set wsData = ThisWorkbook.Worksheets("Data")  'or wherever...

Set tbl = Sheet1.ListObjects("OTJ")

For Each cell In tbl.DataBodyRange.Columns(4).Cells

    Set wb = Workbooks.Open(cell.Value)
    wsData.Cells(2, 5).Value = wb.Sheets("Sheet2").Range("D70").Value
    wb.Close False 'no save
    
Next cell

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM