简体   繁体   English

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

[英]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".尝试将一列的最后 3 个单元格乘以“DATA”工作表中另一列的最后 3 个单元格,然后将它们粘贴到另一个名为“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:这是“报告”表中的结果: 在此处输入图片说明

暂无
暂无

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

相关问题 缩短复制和粘贴 VBA 以将过滤的单元格从一张纸粘贴到另一张纸上 - Shorten Copy and Paste VBA to paste filtered cells from one sheet to another VBA从一张纸复制已命名的单元格,然后粘贴到另一个保留名称 - VBA copy named cells from one sheet and paste to another retaining names VBA 运行时错误 1004 使用.Cells() 从一张纸复制并粘贴到另一张纸上 - VBA Run-time error 1004 whe using .Cells() to copy from one sheet and paste in another Excel VBA-在一张纸上查找并向下粘贴在另一个移动目标单元格上的例程 - Excel VBA - Routine that finds on one sheet and paste on another moving destination cells down 复制M列中以“ xx”开头的单元格,然后将其粘贴到另一张表中 - Copy cells in column M that start with “xx” and paste them into another sheet Excel VBA用于隐藏一个工作表中的单元格(如果它们匹配另一个工作表中的单元格) - Excel VBA for hiding cells in one sheet if they match cells in another sheet 将一张纸的最后 3 个单元格相乘并将它们放在另一张纸中 - Multiplying Last 3 Cells of One Sheet and Placing Them in Another Sheet 尝试将单元格从一张纸复制粘贴到另一张纸上? - Attempting copy paste cells from one sheet to another? Excel可以将某些单元格从一张纸复制/粘贴到另一张纸上,但要稍加改动 - Excel to copy / paste some cells from one sheet to another but with a twist 从一个工作表复制单元格并粘贴到另一个工作表中到可变单元格 - Copy cells from one sheet and paste in another to a variable cell
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM