简体   繁体   中英

copy multiple column from one worksheet to one column in another worksheet

i am having some problems with my code. It all works just fine, until I reach to my 3th foreach statement, then all columns overrides and displays data just from last foreach. Any sugestions please? And I would like to ask if anybody knows how can I use if statement to see if there is a value "stop" in the column ( If ws.cells(i, 1)<> "" Then ). Thank you very much. Below is my code:

    lastRowMaster = 1

For Each Ws In Sheets(Array("List1", "List2", "List3"))
        lastrow = Ws.Range("A" & Rows.Count).End(xlUp).row
         Ws.Range("C1:C50" & lastrow).Copy Destination:=Worksheets("MasterList").Range("D" & lastRowMaster)
         Ws.Range("A1:A50" & lastrow).Copy Destination:=Worksheets("MasterList").Range("A" & lastRowMaster)
        Ws.Range("L1:L50" & lastrow).Copy Destination:=Worksheets("MasterList").Range("B" & lastRowMaster)
         Ws.Range("L1:L50" & lastrow).Copy Destination:=Worksheets("MasterList").Range("C" & lastRowMaster)
        lastRowMaster = Worksheets("MasterList").Range("A" & Rows.Count).End(xlUp).row + 1
Next


For Each Ws In Sheets(Array("List3"))
       lastrow = Ws.Range("N" & Rows.Count).End(xlUp).row
          Ws.Range("Q7:Q50" & lastrow).Copy Destination:=Worksheets("MasterList").Range("D" & lastRowMaster)
         Ws.Range("N7:N50" & lastrow).Copy Destination:=Worksheets("MasterList").Range("A" & lastRowMaster)
        Ws.Range("P7:P50" & lastrow).Copy Destination:=Worksheets("MasterList").Range("B" & lastRowMaster)
         Ws.Range("P7:P50" & lastrow).Copy Destination:=Worksheets("MasterList").Range("C" & lastRowMaster)
         lastRowMaster = Worksheets("MasterList").Range("N" & Rows.Count).End(xlUp).row + 1

Next


For Each Ws In Sheets(Array("List3"))
       lastrow = Ws.Range("AA" & Rows.Count).End(xlUp).row
          Ws.Range("AD7:AD50" & lastrow).Copy Destination:=Worksheets("MasterList").Range("D" & lastRowMaster)
         Ws.Range("AA7:AA50" & lastrow).Copy Destination:=Worksheets("MasterList").Range("A" & lastRowMaster)
        Ws.Range("AC7:AC50" & lastrow).Copy Destination:=Worksheets("MasterList").Range("B" & lastRowMaster)
         Ws.Range("AC7:AC50" & lastrow).Copy Destination:=Worksheets("MasterList").Range("C" & lastRowMaster)
         lastRowMaster = Worksheets("MasterList").Range("AA" & Rows.Count).End(xlUp).row + 1

Next

The second reset for lastRowMaster does not reference the appropriate column on the Worksheets("MasterList") to get the last row.

Dim lr As Long, lrm As Long

lrm = 1
With Worksheets("MasterList")
    For Each Ws In Sheets(Array("List1", "List2", "List3"))
        lr = Ws.Range("A" & Rows.Count).End(xlUp).Row
        Ws.Range("C1:C" & lr).Copy Destination:=.Range("D" & lrm)
        Ws.Range("A1:A" & lr).Copy Destination:=.Range("A" & lrm)
        Ws.Range("L1:L" & lr).Copy Destination:=.Range("B" & lrm)
        Ws.Range("L1:L" & lr).Copy Destination:=.Range("C" & lrm)
        'lrm = .Range("A" & Rows.Count).End(xlUp).Row + 1  '<~~original method; not so good
        lrm = lrm + Range("C1:C" & lr).Rows.Count  `<~~doesn't matter which worksheet. the number of rows remains the same
    Next

    For Each Ws In Sheets(Array("List3"))
        lr = Ws.Range("N" & Rows.Count).End(xlUp).Row
        Ws.Range("Q7:Q" & lr).Copy Destination:=.Range("D" & lrm)
        Ws.Range("N7:N" & lr).Copy Destination:=.Range("A" & lrm)
        Ws.Range("P7:P" & lr).Copy Destination:=.Range("B" & lrm)
        Ws.Range("P7:P" & lr).Copy Destination:=.Range("C" & lrm)
        'lrm = .Range("A" & Rows.Count).End(xlUp).Row + 1 '<~~this was set to column N. Nothing goes into column N.  Probably should be column A from MasterList, not N from ws
        lrm = lrm + Range("Q7:Q" & lr).Rows.Count  `<~~doesn't matter which worksheet. the number of rows remains the same
    Next

    For Each Ws In Sheets(Array("List3"))
        lr = Ws.Range("AA" & Rows.Count).End(xlUp).Row
        Ws.Range("AD7:AD" & lr).Copy Destination:=.Range("D" & lrm)
        Ws.Range("AA7:AA" & lr).Copy Destination:=.Range("A" & lrm)
        Ws.Range("AC7:AC" & lr).Copy Destination:=.Range("B" & lrm)
        Ws.Range("AC7:AC" & lr).Copy Destination:=.Range("C" & lrm)
        'lrm = .Range("AA" & Rows.Count).End(xlUp).Row + 1 '<~~Don't know why this is column AA either. Probably should be column A from MasterList, not AA from ws
        lrm = lrm + Range("AD7:AD" & lr).Rows.Count  `<~~doesn't matter which worksheet. the number of rows remains the same
    Next
End With

Perhaps incrementing lastRowMaster would be better as,

lrm = lrm + Range("AD7:AD" & lr).Rows.Count

I would question all of the code like Ws.Range("C1:C50" & lr) . If lr was 99 then this amounts to Ws.Range("C1:C5099") . Maybe it should be Ws.Range("C1:C" & lr) . I've adjusted my rendition of your code to suit my suspicions.

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