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

我的代码有一些问题。 这一切都运行良好,直到我达到我的第3条foreach语句,然后所有列覆盖并显示数据,从最后的foreach。 任何请求? 我想问问是否有人知道如何使用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的第二次重置不会引用工作表(“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) 。 我已经调整了我的代码,以适应我的怀疑。