using cells for less

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
986
Office Version
  1. 2010
Platform
  1. Windows
Hi,
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
this one is working but still I want to be shorter so this is my approach
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
do not work
thanks .
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Change this
VBA Code:
For j = 2 To 5 Step -1
to this
VBA Code:
For j = 2 To 5

Also this
VBA Code:
Sheets("Sheet2").Cells(2, i)
must be changed to this
Rich (BB code):
Sheets("Sheet2").Cells(2, j)


When you use Step -1 it means to go backwards, but you are starting on 2 and you can't go backwards to 5. So it just stops.
 
Upvote 0
Should be in shorter way:

VBA Code:
Sub TEST()
Dim lr&, i&, ce As Range
Sheets("Sheet2").Activate
With Sheets("Sheet1")
    lr = .Cells(Rows.Count, "F").End(xlUp).Row ' last row in column F
    For Each ce In .Range("F2:F" & lr) ' loop each cell in col F
        For i = 1 To 4
             Cells(ce.Row, "A").Offset(0, i).Value = ce - ce.Offset(0, -i)
        Next
    Next
End With
End Sub
With Cells(ce.Row, "A").Offset(0, i).Value: destination cell in col A sheet 2, but same row with F in sheet 1, offset 1,2,3,4 column
ce.Offset(0, -i) : cell in F then offset left 1,2,3,4 column
 
Upvote 1
Solution

bebo021999

"Big shoutout to Genius bebo021999 🫶 Your consistent invaluable help is MVP-worthy. Thanks for yet another fantastic answer. 🔑 ".​

 
Upvote 0
"Big shoutout to Genius bebo021999 🫶 Your consistent invaluable help is MVP-worthy. Thanks for yet another fantastic answer. 🔑 ".
I truly appreciate your generous words! It's incredibly rewarding to know that my assistance has been valuable to you. Your feedback motivates me to keep offering help to the best of my ability.
If you ever have any more questions or need guidance in the future, don't hesitate to ask.
Your success and satisfaction are what drive me to continue contributing. Thanks again for your kind recognition and have a wonderful day
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,730
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top