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

时间:2015-08-23 08:02:28

标签: excel vba excel-vba

我的代码存在问题。一切正常,直到我达到我的第3个foreach语句,然后所有列都覆盖并显示刚刚从最后一个foreach的数据。请问有什么消化吗?我想问一下是否有人知道如何使用if语句查看列中是否有值“stop”(如果ws.cells(i,1)<>“”那么)。非常感谢你。以下是我的代码:

    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

1 个答案:

答案 0 :(得分:1)

lastRowMaster的第二次重置未引用工作表上的相应列(" 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)。我已根据我的怀疑调整了我的代码版本。