我正在使用下面的脚本,我正在尝试将数据从另一个(目标)工作簿提取到源工作簿中。该脚本正在运行,除了它只拉一行(它应该拉12+)。经过一些调试后,我注意到它只是覆盖了前一行,所以我留下了目标数据的最后一行。我不确定为什么会这样做。我没有任何合并的单元格,源工作簿中没有任何内容。
以下是代码:
Select Code
Dim salesWB As Workbook
Dim mrpWB As Workbook
Dim Ret As Boolean
Dim SalesStaging As String, SW As Range
SalesStaging = "SalesStaging.xlsm"
Set salesWB = Workbooks.Open(SalesStaging)
Set salesWB = Workbooks("SalesStaging.xlsm")
Set mrpWB = ThisWorkbook
With salesWB.Worksheets("SendToMRP")
For Each SW In .Range(.Cells(2, 1), .Cells(Rows.count, 1).End(3))
With mrpWB.Worksheets("Sales & Returns").Cells(Rows.count, 1).End(xlUp)
.Offset(1, 1) = SW.Offset(0, 0) ' OrderId
.Offset(1, 2) = SW.Offset(0, 1) ' StoreFront
.Offset(1, 3) = SW.Offset(0, 2) ' OrderType
.Offset(1, 4) = SW.Offset(0, 3) ' PurchaseDate
.Offset(1, 5) = SW.Offset(0, 4) ' ShipmentServiceLabelCategory
.Offset(1, 6) = SW.Offset(0, 5) ' Title
.Offset(1, 7) = SW.Offset(0, 6) ' SellersSKU
.Offset(1, 8) = SW.Offset(0, 7) ' QtySold
.Offset(1, 9) = SW.Offset(0, 8) ' ShippingCost
.Offset(1, 10) = SW.Offset(0, 9) ' Discounts
.Offset(1, 11) = SW.Offset(0, 10) ' ItemPrice
' .Offset(0, 0) = SW.Offset(0, 11) ' OrderTotal
.Offset(1, 20) = SW.Offset(0, 12) ' LatestShipDate
.Offset(1, 21) = SW.Offset(0, 13) ' SAState
.Offset(1, 22) = SW.Offset(0, 14) ' SACity
.Offset(1, 23) = SW.Offset(0, 15) ' SAPhone
.Offset(1, 24) = SW.Offset(0, 16) ' SAPostalCode
.Offset(1, 25) = SW.Offset(0, 17) ' SAName
.Offset(1, 26) = SW.Offset(0, 18) ' SAddress
.Offset(1, 27) = SW.Offset(0, 19) ' IsPrime
End With
Next
End With
答案 0 :(得分:1)
您正在通过查找A列中的最后一个非空单元格来确定要写入哪一行,但您从不向A列写入任何内容,因此每次循环时您将始终选择相同的行。
尝试以下方法:
Dim salesWB As Workbook
Dim mrpWB As Workbook
Dim Ret As Boolean
Dim SalesStaging As String, SW As Range
SalesStaging = "SalesStaging.xlsm"
Set salesWB = Workbooks.Open(SalesStaging)
Set salesWB = Workbooks("SalesStaging.xlsm")
Set mrpWB = ThisWorkbook
With salesWB.Worksheets("SendToMRP")
For Each SW In .Range(.Cells(2, 1), .Cells(.Rows.count, 1).End(3))
With mrpWB.Worksheets("Sales & Returns").Cells(mrpWB.Worksheets("Sales & Returns").Rows.count, 2).End(xlUp)
.Offset(1, 0) = SW.Offset(0, 0) ' OrderId
.Offset(1, 1) = SW.Offset(0, 1) ' StoreFront
.Offset(1, 2) = SW.Offset(0, 2) ' OrderType
.Offset(1, 3) = SW.Offset(0, 3) ' PurchaseDate
.Offset(1, 4) = SW.Offset(0, 4) ' ShipmentServiceLabelCategory
.Offset(1, 5) = SW.Offset(0, 5) ' Title
.Offset(1, 6) = SW.Offset(0, 6) ' SellersSKU
.Offset(1, 7) = SW.Offset(0, 7) ' QtySold
.Offset(1, 8) = SW.Offset(0, 8) ' ShippingCost
.Offset(1, 9) = SW.Offset(0, 9) ' Discounts
.Offset(1, 10) = SW.Offset(0, 10) ' ItemPrice
' .Offset(0, 0) = SW.Offset(0, 11) ' OrderTotal
.Offset(1, 19) = SW.Offset(0, 12) ' LatestShipDate
.Offset(1, 20) = SW.Offset(0, 13) ' SAState
.Offset(1, 21) = SW.Offset(0, 14) ' SACity
.Offset(1, 22) = SW.Offset(0, 15) ' SAPhone
.Offset(1, 23) = SW.Offset(0, 16) ' SAPostalCode
.Offset(1, 24) = SW.Offset(0, 17) ' SAName
.Offset(1, 25) = SW.Offset(0, 18) ' SAddress
.Offset(1, 26) = SW.Offset(0, 19) ' IsPrime
End With
Next
End With