Hello,
I wanted to automate calculations of the sumproduct of two arrays of variables which could iterate for dozens/hundreds of times, and then copy the results to a column, and then repeat the process, and copy the new results to a column next to the previous one. Upon completion of all the iterations, I wanted to add up all the results copied in the columns by row, and copy the sum of each row to each cell in a designated range one by one.
The code seems to be running without warnings, but the major issue seems to be that after each calculation, the results that are copied to the columns would be overwritten by the following calculated results. Also, the calculations seem to be taking some time to complete although it seems to be simple calculations.
Any help with reviewing and advising the following codes is much apprecited!
Sub Macro1() ' ' Macro1 Macro ' calculate total emissions from transport '
Dim rowcount As Long
rowcount = Worksheets("Building Components").Range("Transport_Raw_Materials").Rows.Count
rowcount_results_total = Worksheets("Building Components").Range("TD_RawMaterial_Results_Total").Rows.Count rowcount_results_urban = Worksheets("Building Components").Range("TD_RawMaterial_Results_Urban").Rows.Count
rowcount_results_total_items = Worksheets("Factors_T&D").Range("TD_RawMaterial_Results_Total_Items").Rows.Count rowcount_results_urban_items = Worksheets("Factors_T&D").Range("TD_RawMaterial_Results_Urban_Items").Rows.Count
Dim i As Integer
For i = 1 To rowcount
Worksheets("Factors_T&D").Range("Truck_ModeShare_Raw_Material") = Worksheets("Building Components").Range("Transport_Raw_Materials").Cells(i, 2).Value
Worksheets("Factors_T&D").Range("Rail_ModeShare_Raw_Material") = Worksheets("Building Components").Range("Transport_Raw_Materials").Cells(i, 3).Value
Worksheets("Factors_T&D").Range("Tanker_ModeShare_Raw_Material") = Worksheets("Building Components").Range("Transport_Raw_Materials").Cells(i, 4).Value
Worksheets("Factors_T&D").Range("Truck_Dist_Raw_Material") = Worksheets("Building Components").Range("Transport_Raw_Materials").Cells(i, 5).Value
Worksheets("Factors_T&D").Range("Rail_Dist_Raw_Material") = Worksheets("Building Components").Range("Transport_Raw_Materials").Cells(i, 6).Value
Worksheets("Factors_T&D").Range("Tanker_Dist_Raw_Material") = Worksheets("Building Components").Range("Transport_Raw_Materials").Cells(i, 7).Value
ActiveCell.FormulaR1C1 = "=SUMPRODUCT(RC[" & -3 - i & "]:RC[" & -1 - i & "],R38C27:R38C29)" 'This is where the sumproduct calculation occurs, but I would like to have the calculation go regardless of which particular active cell my mouse pointer in the spreadsheet happens to be'
Worksheets("Factors_T&D").Calculate
Range("Ae53").Offset(0, i - 1).Select
Selection.AutoFill Destination:=Range("Ae53:Ae58").Offset(0, i - 1), Type:=xlFillDefault
Range("Ae53:Ae58").Offset(0, i - 1).Select
Range("Ae58").Offset(0, i - 1).Select
Selection.Copy
Range("Ae60:Ae70").Offset(0, i - 1).Select
ActiveSheet.Paste
ActiveWindow.SmallScroll Down:=12
Range("Ae72:Ae79").Offset(0, i - 1).Select
ActiveSheet.Paste
ActiveWindow.SmallScroll Down:=-12
Application.CutCopyMode = False
Range("Ae52").Offset(0, i - 1).Value = Worksheets("Building Components").Range("List_Raw_Materials").Cells(i).Value
Range("Ae53").Offset(0, i).Select
Next i
For i = 1 To rowcount_results_total Worksheets("Building Components").Range("TD_RawMaterial_Results_Total").Cells(i) = WorksheetFunction.Sum(Worksheets("Factors_T&D").Range(Range("TD_RawMaterial_Results_Total_Items").Cells(i), Range("TD_RawMaterial_Results_Total_Items").Cells(i).End(xlToRight))) Next i
For i = 1 To rowcount_results_urban Worksheets("Building Components").Range("TD_RawMaterial_Results_Urban").Cells(i) = WorksheetFunction.Sum(Worksheets("Factors_T&D").Range(Range("TD_RawMaterial_Results_Urban_Items").Cells(i), Range("TD_RawMaterial_Results_Urban_Items").Cells(i).End(xlToRight))) Next i
Calculate
End Sub
I wanted to automate calculations of the sumproduct of two arrays of variables which could iterate for dozens/hundreds of times, and then copy the results to a column, and then repeat the process, and copy the new results to a column next to the previous one. Upon completion of all the iterations, I wanted to add up all the results copied in the columns by row, and copy the sum of each row to each cell in a designated range one by one.
The code seems to be running without warnings, but the major issue seems to be that after each calculation, the results that are copied to the columns would be overwritten by the following calculated results. Also, the calculations seem to be taking some time to complete although it seems to be simple calculations.
Any help with reviewing and advising the following codes is much apprecited!
Sub Macro1() ' ' Macro1 Macro ' calculate total emissions from transport '
Dim rowcount As Long
rowcount = Worksheets("Building Components").Range("Transport_Raw_Materials").Rows.Count
rowcount_results_total = Worksheets("Building Components").Range("TD_RawMaterial_Results_Total").Rows.Count rowcount_results_urban = Worksheets("Building Components").Range("TD_RawMaterial_Results_Urban").Rows.Count
rowcount_results_total_items = Worksheets("Factors_T&D").Range("TD_RawMaterial_Results_Total_Items").Rows.Count rowcount_results_urban_items = Worksheets("Factors_T&D").Range("TD_RawMaterial_Results_Urban_Items").Rows.Count
Dim i As Integer
For i = 1 To rowcount
Worksheets("Factors_T&D").Range("Truck_ModeShare_Raw_Material") = Worksheets("Building Components").Range("Transport_Raw_Materials").Cells(i, 2).Value
Worksheets("Factors_T&D").Range("Rail_ModeShare_Raw_Material") = Worksheets("Building Components").Range("Transport_Raw_Materials").Cells(i, 3).Value
Worksheets("Factors_T&D").Range("Tanker_ModeShare_Raw_Material") = Worksheets("Building Components").Range("Transport_Raw_Materials").Cells(i, 4).Value
Worksheets("Factors_T&D").Range("Truck_Dist_Raw_Material") = Worksheets("Building Components").Range("Transport_Raw_Materials").Cells(i, 5).Value
Worksheets("Factors_T&D").Range("Rail_Dist_Raw_Material") = Worksheets("Building Components").Range("Transport_Raw_Materials").Cells(i, 6).Value
Worksheets("Factors_T&D").Range("Tanker_Dist_Raw_Material") = Worksheets("Building Components").Range("Transport_Raw_Materials").Cells(i, 7).Value
ActiveCell.FormulaR1C1 = "=SUMPRODUCT(RC[" & -3 - i & "]:RC[" & -1 - i & "],R38C27:R38C29)" 'This is where the sumproduct calculation occurs, but I would like to have the calculation go regardless of which particular active cell my mouse pointer in the spreadsheet happens to be'
Worksheets("Factors_T&D").Calculate
Range("Ae53").Offset(0, i - 1).Select
Selection.AutoFill Destination:=Range("Ae53:Ae58").Offset(0, i - 1), Type:=xlFillDefault
Range("Ae53:Ae58").Offset(0, i - 1).Select
Range("Ae58").Offset(0, i - 1).Select
Selection.Copy
Range("Ae60:Ae70").Offset(0, i - 1).Select
ActiveSheet.Paste
ActiveWindow.SmallScroll Down:=12
Range("Ae72:Ae79").Offset(0, i - 1).Select
ActiveSheet.Paste
ActiveWindow.SmallScroll Down:=-12
Application.CutCopyMode = False
Range("Ae52").Offset(0, i - 1).Value = Worksheets("Building Components").Range("List_Raw_Materials").Cells(i).Value
Range("Ae53").Offset(0, i).Select
Next i
For i = 1 To rowcount_results_total Worksheets("Building Components").Range("TD_RawMaterial_Results_Total").Cells(i) = WorksheetFunction.Sum(Worksheets("Factors_T&D").Range(Range("TD_RawMaterial_Results_Total_Items").Cells(i), Range("TD_RawMaterial_Results_Total_Items").Cells(i).End(xlToRight))) Next i
For i = 1 To rowcount_results_urban Worksheets("Building Components").Range("TD_RawMaterial_Results_Urban").Cells(i) = WorksheetFunction.Sum(Worksheets("Factors_T&D").Range(Range("TD_RawMaterial_Results_Urban_Items").Cells(i), Range("TD_RawMaterial_Results_Urban_Items").Cells(i).End(xlToRight))) Next i
Calculate
End Sub