The task is a series of copy and pastes from worksheet A to worksheet B and then one copy of a newly calculated cell from worksheet B back into worksheet A. Think of the 3 values from worksheet A as being model inputs and worksheet B is the model. Once they are entered the model creates the output value which I then want to place nest to the corresponding row of inputs in worksheet A.
- Cell G2 in worksheet A is numeric and must go to cell G25 in worksheet B.
- Cell H2 in worksheet A is also numeric and must go to cell G30 in worksheet B.
- Cell C2 in worksheet A is text and must be pasted (paste special’d) into cell C5 in worksheet B.
- Finally the output variable (cell D3) from worksheet B must be pasted back into cell I2 in worksheet A.
The number of rows in worksheet A happens to be 4,040 (rows 2 to 4,041) in this case but can be any number going forward.
Here is mu code currently:
Sub CopyPasteLoop3()
'note: Long refers to a datatype for potentially long numbers
Dim i As Long
Dim wb As Workbook
Dim sht1 As Worksheet
Dim sht2 As Worksheet
Set wb = ThisWorkbook
Set sht1 = wb.Sheets("Sheet13")
Set sht2 = wb.Sheets("Sheet2")
'loop from Row2 to last row with a value in Column G
For i = 2 To sht1.Cells(Rows.Count, "G").End(x1Up).Row
'assign the 3 inputs
sht2.Range("G25").Value = sht1.Cells(i, "G").Value
sht2.Range("G30").Value = sht1.Cells(i, "H").Value
sht2.Range("C5").Value = sht1.Cells(i, "C").Value
sht2.Calculate
'copy the output
sht1.Cells(i, "I").Value = sht2.Range("D3").Value
Next i
End Sub
End(x1Up)should beEnd(xlUp)