繁体   English   中英

将多列从一个工作表复制到另一工作表的一列

[英]copy multiple column from one worksheet to one column in another worksheet

我的代码有问题。 一切正常,直到我到达第三个foreach语句,然后所有列都覆盖并仅显示最后一个foreach的数据。 有什么建议吗? 我想问问是否有人知道如何使用if语句查看列中是否有值“ stop”(If ws.cells(i,1)<>“” Then)。 非常感谢你。 下面是我的代码:

    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

lastRowMaster的第二次重置未引用Worksheets(“ MasterList”)上的相应列以获取最后一行。

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

也许增加lastRowMaster会更好,因为,

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

我会质疑所有类似Ws.Range("C1:C50" & lr) 如果lr为99,则等于Ws.Range("C1:C5099") 也许应该是Ws.Range("C1:C" & lr) 我已经调整了对您代码的解释,以适应我的怀疑。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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