繁体   English   中英

VBA 将一张纸中的单元格相乘并将它们粘贴到另一张纸上

[英]VBA Multiply Cells in One Sheet and Paste Them to Another

尝试将一列的最后 3 个单元格乘以“DATA”工作表中另一列的最后 3 个单元格,然后将它们粘贴到另一个名为“Report”的工作表上。

我有这个看起来不正确的代码,但也许你可以帮忙? :)

Sheets("DATA").Activate
' Use this lRow now since the previous one require you to be in a With loop
Range("A" & lRow - 2 & ":A" & lRow).Copy

With Sheets("Report")
.Activate
' Pastes the last 3 cells of Column A into the Month column
.Range("B9").PasteSpecial Paste:=xlPasteAll
.Range("B8").Formula = "Month"
.Range("C8").Formula = "Production Cost"
' Calculates the Production cost
.Range(.Cells(lRow - 3, 2), .Cells(lRow, 2)).Copy
.Range("C9").AutoFill Destination:=Range("C9:C11")
' Calculates the Inventory cost
.Range("C14").Select
.Range("D8").Formula = "Inventory Cost"
.Range("E8").Formula = "Total Cost"
.Range("B12").Formula = "Total"
End With
Sub asd()

'Fills the labels of the table
Sheets("report").Range("B8").Value = "Month"
Sheets("report").Range("C8").Value = "Production Cost"
Sheets("report").Range("D8").Value = "Inventory Cost"
Sheets("report").Range("E8").Value = "Total Cost"
Sheets("report").Range("B12").Value = "Total"
'Fills the labels of the table

lastrow = Sheets("data").Range("A1").End(xlDown).Row
For i = 2 To 0 Step -1
    Sheets("report").Cells(lastrow - i - 2, 2).Value = Sheets("data").Cells(lastrow - i, 1).Value
    Sheets("report").Cells(lastrow - i - 2, 3).Value = Sheets("data").Cells(lastrow - i, 2).Value * Sheets("data").Cells(lastrow - i, 4).Value
    Sheets("report").Cells(lastrow - i - 2, 4).Value = Sheets("data").Cells(lastrow - i, 3).Value * Sheets("data").Cells(lastrow - i, 5).Value
    Sheets("report").Cells(lastrow - i - 2, 5).Value = Sheets("report").Cells(lastrow - i - 2, 3).Value + Sheets("report").Cells(lastrow - i - 2, 4).Value
Next i

Sheets("report").Range("C12").Formula = "=SUM(C9:C11)"
Sheets("report").Range("D12").Formula = "=SUM(D9:D11)"
Sheets("report").Range("E12").Formula = "=SUM(E9:E11)"
End Sub

试试这个。 它不是一个真正通用的代码,因此如果您遇到问题,请告诉我,这很可能是因为您的数据和我的数据位于不同的位置。 我正在分享我的“数据”表,您可以用作参考。 在此处输入图片说明

这是“报告”表中的结果: 在此处输入图片说明

暂无
暂无

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

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