montecarlo2012
Well-known Member
- Joined
- Jan 26, 2011
- Messages
- 986
- Office Version
- 2010
- Platform
- Windows
Hi,
I am trying to make it shorter
this one is working but still I want to be shorter so this is my approach
do not work
thanks .
I am trying to make it shorter
VBA Code:
Sub F_minus_E()
Dim lastRow As Long
Dim i As Long
' Find the last row in column F on Sheet1 (you can change the column as needed)
lastRow = Sheets("Sheet1").Cells(Sheets("Sheet1").Rows.Count, "F").End(xlUp).Row
' Display the results on Sheet2 F2-E2 #### -D2 ##### -C2 @@@@ -B2
For i = 1 To lastRow
' Perform calculations for each row
Sheets("Sheet2").Range("B2").Offset(i - 1, 0).Value = Sheets("Sheet1").Range("F2").Offset(i - 1, 0).Value - Sheets("Sheet1").Range("E2").Offset(i - 1, 0).Value
Sheets("Sheet2").Range("C2").Offset(i - 1, 0).Value = Sheets("Sheet1").Range("F2").Offset(i - 1, 0).Value - Sheets("Sheet1").Range("D2").Offset(i - 1, 0).Value
Sheets("Sheet2").Range("D2").Offset(i - 1, 0).Value = Sheets("Sheet1").Range("F2").Offset(i - 1, 0).Value - Sheets("Sheet1").Range("C2").Offset(i - 1, 0).Value
Sheets("Sheet2").Range("E2").Offset(i - 1, 0).Value = Sheets("Sheet1").Range("F2").Offset(i - 1, 0).Value - Sheets("Sheet1").Range("B2").Offset(i - 1, 0).Value
Next i
End Sub
VBA Code:
Sub montecarlo()
lastRow = Sheets("Sheet1").Cells(Sheets("Sheet1").Rows.Count, "F").End(xlUp).Row
For i = 2 To lastRow
For j = 2 To 5 Step -1
Sheets("Sheet2").Cells(2, i).Offset(i - 1, 0).Value = Sheets("Sheet1").Cells(2, 6).Offset(i - 1, 0).Value - Sheets("Sheet1").Cells(j, 2).Offset(i - 1, 0).Value
Next
Next
End Sub
thanks .