简体   繁体   中英

Copy data from one sheet to another based on cell value

I am trying to copy data from one sheet to another in the same workbook based on a cell value: below is my code, however it gives me an application 1004 error when compiling and i do not have a clue. please he;lp

Sub COPY1()
     Dim lROW As Integer
     Dim rngFrom, rngTo As Range
     Sheets("LEL2230K").Activate
lROW = Sheets("LEL2230K").Cells(Rows.Count, "C").End(xlUp).Row
Set rngFrom = Sheets("LEL2230K").Range(Cells(6, "C"), Cells(lROW, "C"))
 Sheets("MASTER").Activate
Set rngTo = Sheets("Master").Range(Cells(7, "A"), Cells(lROW, "A"))
rngFrom.Copy rngTo


End Sub

I created two sheets with same names.

I put in sheet "LEL2230K" address C6:C9 some data: "DATA1", "DATA2", "DATA3", "DATA4"

When I run your macro, everything works fine. But I make some changes in your code, try it:

Sub COPY1()

    Dim LEL2230K, MASTER As Worksheet

    Set LEL2230K = ThisWorkbook.Sheets("LEL2230K")
    Set MASTER = ThisWorkbook.Sheets("MASTER")

    LEL2230K.Range(LEL2230K.Cells(6, "C"), LEL2230K.Cells(LEL2230K.Cells(LEL2230K.Rows.Count, "C").End(xlUp).Row, "C")).Copy

    MASTER.Cells(7, "A").PasteSpecial

End Sub

I highly recommend you to name sheet objects in it properties and use it name as reference.

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