Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 将一张图纸的最后一行相乘并放置到另一张图纸上_Excel_Vba - Fatal编程技术网

Excel 将一张图纸的最后一行相乘并放置到另一张图纸上

Excel 将一张图纸的最后一行相乘并放置到另一张图纸上,excel,vba,Excel,Vba,尝试将我的“数据”表中的2列最后3行“生产单位”与“生产成本”相乘,并将它们放在“生产成本”列标题下的“报告”表中。我一直在努力,但没用。谢谢 我的电子表格如下所示: 我的代码: Sheets("DATA").Activate ' Use this lRow now since the previous one require you to be in a With loop Range(Cells(lRow - 2, 1), Cells(lRow, 1)).Copy With She

尝试将我的“数据”表中的2列最后3行“生产单位”与“生产成本”相乘,并将它们放在“生产成本”列标题下的“报告”表中。我一直在努力,但没用。谢谢

我的电子表格如下所示:

我的代码:

Sheets("DATA").Activate
' Use this lRow now since the previous one require you to be in a With   loop
Range(Cells(lRow - 2, 1), Cells(lRow, 1)).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
End Sub

这是您的代码的工作版本。您可能想看看语法

    Dim WsData As Worksheet
Dim WsReport As Worksheet
Dim Rl As Long                          ' last row
Dim Rng As Range

Set WsData = Worksheets("Data")
Set WsReport = Worksheets("Report")

With WsData
    ' look for the last used cell in column B
    Rl = .Cells(.Rows.Count, "A").End(xlUp).Row
    Set Rng = .Range(.Cells(Rl - 2, 1), .Cells(Rl, 1))
    ' Pastes the last 3 cells of Column A into the Month column
    Rng.Copy Destination:=WsReport.Cells(9, "B")
End With

With WsReport
    .Cells(8, "B").Formula = "Month"
    .Cells(8, "C").Formula = "Production Cost"
    .Cells(8, "D").Formula = "Month"
    .Cells(8, "E").Formula = "Inventory Cost"
    .Cells(8, "F").Formula = "Total cost"
    .Cells(12, "B").Formula = "Total"

    ' Calculates the Production cost
    '   there is no calculation unless there are formulas being copied
    Rng.Offset(0, 1).Copy Destination:=.Cells(9, "C")

    ' Calculates the Inventory cost
    '   there is no calculation
End With

当然,所有的计算都丢失了,因为它们没有包含在您的问题中。我希望您能够将它们添加到我提供的代码中。

您到底在尝试什么?请在你的问题中包括这一点。把它写在帖子里。我以为我已经这么做了。我认为问题在于,我希望读取数据的工作表“数据”未激活。无需激活它。。。您是否尝试过:
工作表(“数据”)。范围(“A”&lRow-2&“:A”&lRow)。复制
?我尝试过,但没有效果。应该放置值的单元格保持为空。要进行复制,请将这段代码放入托盘:
Dim shD As Worksheet
Set shD=Sheets(“数据”)
shD.Range(shD.cells(lRow-2,1),shD.cells(lRow,1))。复制Sheets(“Report”).Range(“B9”)
。注意:
表格(“报告”)。范围(“B9”)
必须与复印件在同一行。只被一个空格隔开。