I'm trying to multiply 2 values in another worksheet named "DATA" and then multiply 2 more values underneath it. This must be expandable so when more values are added to the spreadhseet, the cells with the calculations update. Those calculations are placed in a worksheet called "Report" in cells C9:C11.
I'm wanting to multiply cell 45 of column B by cell 45 of column C. Then, multiply cell 46 of column B by cell 46 of column C, finally multiply cell 47 of column b by cell 47 of column c.
If new data is added for example another 3 cells to columns B and C, this should update to accommodate the data and multiply those values.
My code which is static:
Range("C9").Select
ActiveCell.FormulaR1C1 = "=DATA!R[30]C[-1]*DATA!R[30]C"
Range("C9").AutoFill Destination:=Range("C9:C11"), Type:=xlFillDefault
Spreadsheet: https://imgur.com/a/32hXkDk Where calculations should go: https://i.stack.imgur.com/T61IS.png
Code:
Sub HourStraightOnetoTwo()
Dim lRow As Integer
Dim rangex As Range, rangey As Range, rangey2 As Range, rangey3 As Range, rangey4 As Range
Dim rangexMonth As Range, rangeInvNorm As Range, rangeInvFore As Range
Dim srs As Series
Dim ws As Worksheet
' Selects the entirety of column A and formats the date to be in the format of mmm-dd
Columns("A:A").Select
Selection.NumberFormat = "[$-en-US]mmm-yy;@"' Deletes the sheet named Report if it exists
For Each ws In Sheets
If ws.Name = "Report" Then
Application.DisplayAlerts = False
Sheets("Report").Delete
Application.DisplayAlerts = True
Exit For
End If
Next
With Sheets("DATA")
.Activate
' Adds titles to forecast columns
.Range("J1").Value = "Production Forecast"
.Range("K1").Value = "Demand Forecast"
.Range("L1").Value = "Inventory Forecast"' Selects the Last Row
lRow = Worksheets("DATA").Range("B"& Rows.Count).End(xlUp).Row
' Copies the last 3 cells in column 2, Production Units
.Range(.Cells(lRow - 3, 2), .Cells(lRow, 2)).Copy
' Pastes those last 3 cells in column 10, Production Forecast
.Range(.Cells(lRow - 3, 10), .Cells(lRow, 10)).PasteSpecial Paste:=xlPasteValues
' Copies the last 3 cells in column 5, Demand
.Range(.Cells(lRow - 3, 5), .Cells(lRow, 5)).Copy
' Pastes those last 3 cells in column 11, Demand Forecast
.Range(.Cells(lRow - 3, 11), .Cells(lRow, 11)).PasteSpecial Paste:=xlPasteValues
' Calculates Inventory
lRow = .Cells(.Rows.Count, "A").End(xlUp).Row
' Subtracts Production Units from Demand
.Cells(2, "G").FormulaR1C1 = "=RC[-5]-RC[-2]"' Subtracts Production Units from Demand and adds the previous month's Inventory
.Range(.Cells(3, "G"), .Cells(lRow, "G")).FormulaR1C1 = "=RC[-5]-RC[-2]+R[-1]C"' Calculates next 3 months of Inventory - Redundant but needed for how this is coded
.Range(Cells(lRow - 3, 12), Cells(lRow, 12)).FormulaR1C1 = "=RC[-2]-RC[-1]+R[-1]C[-4]"
.Range(Cells(lRow - 2, 12), Cells(lRow, 12)).FormulaR1C1 = "=RC[-2]-RC[-1]+R[-1]C"
End With
' Add the Report worksheet
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Report"
With Sheets("Report")
.Range("A2") = Now()
.Range("A3").Value = "Dear Madam/Sir,"
.Range("A5").Value = "The following are the projected production, inventory, and total costs for the following"
.Range("A6").Value = "three months."
End With
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
' THIS IS A WORK IN PROGRESS
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;"
.Range("A15").Formula = "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"
.Range("A29").Formula = "following three months."
.Range("A41").Formula = "Regards,"
.Range("A42").Formula = "name"
.Range("A43").Formula = "email"' Calculates the Production cost
Range("C9").Select
ActiveCell.FormulaR1C1 = "=DATA!R[30]C[-1]*DATA!R[30]C"
Range("C9").AutoFill Destination:=Range("C9:C11"), Type:=xlFillDefault
' Calculates the Inventory cost
Range("D9").Select
ActiveCell.FormulaR1C1 = "=DATA!R[30]C[3]*DATA!R[30]C"
Range("D9").AutoFill Destination:=Range("D9:D11"), Type:=xlFillDefault
Range("D10").Select
.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