简体   繁体   中英

Copy multiple rows from one excel file to another using VBA

I have a large excel file (Around 10 MB) that is written out as a very long list of tables. I want to create a macro on a separate file that searches through the first file for a certain value and then returns the appropriate table. So far i have gotten the search functionality to work and i can return a single cell using offset, but when i go to return multiple records i receive a couple of different errors.

The section of the code i am currently trying to use to copy the cells is:

d = c + 7
For Counter = c To d
    objWorkbook.Sheets("Sheetname").Range(Cells(Counter, "A"), Cells(Counter, "M")).Value = Range("M3:Y10").Value
Next

In this case it should take each row and copy it to the section specified ( c is the cell currently being checked and d is the row number of the bottom of each seperate table) but instead i get the following error:

Run-time error '1004' Application-defined or object-defined error

I am fairly new at using VBA so i'm not sure if the error is very simple and i'm just not seeing it properly.

Cells(Counter, "A") , Cells(Counter, "M") , and Range("M3:Y10") have no Workbook or Worksheet, so it is defaulting to the active one.

d = c + 7
For Counter = c To d
    with objWorkbook.Sheets("Sheetname")
       'Not sure what the other workbook and sheet are called
       OtherWorkbook.Sheets("SheetnameHere").Range("M3:Y10").Value = .Range(.Cells(Counter, "A"), .Cells(Counter, "M")).Value 
    end with
Next

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