将一张纸的最后3个单元格相乘并将它们放在另一张纸中

问题描述 投票:0回答:1

我要完成的工作:-将表“ DATA”中B和C列的最后3个单元格相乘-将这些值放在“报表”工作表的单元格C9:C11中-问题是我不知道如何告诉Excel执行此操作并将这些值移动到另一张工作表。

这是到目前为止我必须要做的代码:

' Calculates the Production cost
.Range(.Cells(lRow - 3, 2), .Cells(lRow, 2)).Copy
.Range("C9").AutoFill Destination:=Range("C9:C11")

我的其余代码:

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
Columns("A:A").ColumnWidth = 13.71
Columns("C:C").ColumnWidth = 13.71
Columns("D:D").ColumnWidth = 13.71
.Range("B8").Formula = "Month"
.Range("C8").Formula = "Production Cost"
.Range("A14").Formula = "Figure below illustrates the production unit, and the demand time series for the past year; as well as the forecast for the following three months."
.Range("A28").Formula = "Figure below illustrates the inventory levels for the past year, as well as the forecast for the following three months."
.Range("A42").Formula = "Regards,"
' 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"
' Sums Production Cost for the last 3 months
.Range("C12").Formula = "=SUM(C9:C11)"
' Fills in the adjacent cells to the right
.Range("C12").AutoFill Destination:=Range("C12:E12"), Type:=xlFillDefault
' Sums Production and Inventory Cost for one month
.Range("E9").Formula = "=SUM(C9:D9)"
' Fills in the cells below
.Range("E9").AutoFill Destination:=Range("E9:E11"), Type:=xlFillDefault
End With

我的电子表格:

enter image description here

enter image description here

excel vba
1个回答
0
投票

清理了很多。我没有打扰循环sum单元。您的代码也缺少Inventory Cost

的计算
Sub test()
    Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = False

    Dim lRow As Long
    Dim wb As Workbook: Set wb = ThisWorkbook
    Dim Sourcews As Worksheet: Set Sourcews = wb.Worksheets("DATA")
    Dim Destinationws As Worksheet: Set Destinationws = wb.Worksheets("Report")

    With Sourcews
        lRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        .Range(.Cells(lRow - 2, 1), .Cells(lRow, 1)).Copy Destination:=Destinationws.Cells(9, 2)
        .Range(.Cells(lRow - 2, 3), .Cells(lRow, 3)).Copy Destination:=Destinationws.Cells(9, 3)
        .Range(.Cells(lRow - 2, 7), .Cells(lRow, 7)).Copy Destination:=Destinationws.Cells(9, 4)
    End With

    With Destinationws
        ' text
        .Cells(12, 2).Value = "Total"
        .Cells(8, 2).Value = "Month"
        .Cells(8, 3).Value = "Production Cost"
        .Cells(8, 4).Value = "Inventory Cost"
        .Cells(8, 5).Value = "Total Cost"
        .Cells(8, 3).Value = "Production Cost"
        .Cells(14, 1).Value = "Figure below illustrates the production unit, and the demand time series for the past year; as well as the forecast for the following three months."
        .Cells(28, 1).Value = "Figure below illustrates the inventory levels for the past year, as well as the forecast for the following three months."
        .Cells(42, 1).Value = "Regards,"
        ' Inventory Cost calculation needed
        .Cells(9, 4).Value = .Cells(9, 4).Value ' * another value?
        .Cells(10, 4).Value = .Cells(10, 4).Value ' * another value?
        .Cells(11, 4).Value = .Cells(11, 4).Value ' * another value?
        ' footer totals
        .Cells(12, 3).Value = WorksheetFunction.Sum(.Cells(9, 3), .Cells(10, 3), .Cells(11, 3))
        .Cells(12, 4).Value = WorksheetFunction.Sum(.Cells(9, 4), .Cells(10, 4), .Cells(11, 4))
        .Cells(12, 5).Value = WorksheetFunction.Sum(.Cells(12, 3), .Cells(12, 4))
        ' right totals
        .Cells(9, 5).Value = WorksheetFunction.Sum(.Cells(9, 3), .Cells(9, 4))
        .Cells(10, 5).Value = WorksheetFunction.Sum(.Cells(10, 3), .Cells(10, 4))
        .Cells(11, 5).Value = WorksheetFunction.Sum(.Cells(11, 3), .Cells(11, 4))
        .Columns(2).Resize(, 5).ColumnWidth = 13.71
    End With
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.