[英]Excel to copy / paste some cells from one sheet to another but with a twist
[英]Copy data from one sheet and paste to another (under some conditions)
我的宏從幾張紙(特定的一張紙叫做SS)中復制數據,並將其粘貼到另一張紙(MainSheet)中:
Dim lastrow As Long
'(...)
Lastrowss = Sheets (“SS”).Range(“A1”).End(xlDown).Row
'(...)
For i = 2 To lastrows
' (...)
Sheets("MainSheet").Cells(j, 7).Value = Sheets("SS").Cells(i, 7).Value 'Rec Amount
Sheets("MainSheet").Cells(j, 9).Value = Sheets("SS").Cells(i, 9).Value * -1 'Paid Amount
該宏僅適用於賣出 。 購買以相反的方式反映出來。
僅當D列中有“購買”時,宏才應從第7列和第9列復制數據並將其粘貼到第9列和第7列(而不是第7列和第9列)。如果有“出售”,則數據應保持不變。
在D列中,您並不總是僅獲得“出售”或“購買”。 您還可以看到“賣出掩蓋”等信息。但是邏輯保持不變。
If Sheets("SS").Cells(i, 4).Value <> "buy" Then ' <-- add this test
Sheets("MainSheet").Cells(j, 7).Value = Sheets("SS").Cells(i, 7).Value 'Rec Amount
Sheets("MainSheet").Cells(j, 9).Value = Sheets("SS").Cells(i, 9).Value * -1 'Paid Amount
Else ' <-- here we inverse the two columns
Sheets("MainSheet").Cells(j, 7).Value = Sheets("SS").Cells(i, 9).Value 'Rec Amount
Sheets("MainSheet").Cells(j, 9).Value = Sheets("SS").Cells(i, 7).Value * -1 'Paid Amount
End If
謝謝大家的幫助。 我稍微更新了代碼:
If Left(Sheets("SS").Cells(i, 4).Value, 3) <> "Buy" Then
Sheets("MainSheet").Cells(j, 7).Value = Sheets("SS").Cells(i, 7).Value 'Rec Amount
Sheets("MainSheet").Cells(j, 9).Value = Sheets("SS").Cells(i, 9).Value * -1 'Paid Amount
Else
Sheets("MainSheet").Cells(j, 7).Value = Sheets("SS").Cells(i, 9).Value 'Rec Amount
Sheets("MainSheet").Cells(j, 9).Value = Sheets("SS").Cells(i, 7).Value * -1 'Paid Amount
End If
似乎現在可以工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.