简体   繁体   中英

VBA Multiply Cells in One Sheet and Paste Them to Another

Trying to multiply the last 3 cells of a column by the last 3 cells of another column in the "DATA" worksheet and paste them onto another worksheet named "Report".

I have this code which doesn't look correct but maybe you can help? :)

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

Try this one out. It is not a really versatile code so if you have trouble with it let me know, it is most probably because your data and mine are in different locations. I'm sharing my "data" sheet, you can use as a reference. 在此处输入图片说明

And here is the result in the "report" sheet: 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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