How about a better way to write this

Nlhicks

Board Regular
Joined
Jan 8, 2021
Messages
244
Office Version
  1. 365
Platform
  1. Windows
(What makes this more difficult is that we are only interested in every other row in the main range). Would it be:

For i= 11 to 18
If .Range("B" & i)<>.Range("E" & i) Then
.Range("L11" & i).Value = .Range("E" & i)-.Range("B" & i)
End If
Next i




If Worksheets("Line Update").Range("B11") <> Worksheets("Line Update").Range("E11") Then

Worksheets("Line Update").Range("L11").Value = Worksheets("Line Update").Range("E11") - Worksheets("Line Update").Range("B11")

End If

If Worksheets("Line Update").Range("B13") <> Worksheets("Line Update").Range("E13") Then

Worksheets("Line Update").Range("L12").Value = Worksheets("Line Update").Range("E11") - Worksheets("Line Update").Range("B13")

End If

If Worksheets("Line Update").Range("B15") <> Worksheets("Line Update").Range("E15") Then

Worksheets("Line Update").Range("L13").Value = Worksheets("Line Update").Range("E15") - Worksheets("Line Update").Range("B15")

End If
If Worksheets("Line Update").Range("B17") <> Worksheets("Line Update").Range("E17") Then

Worksheets("Line Update").Range("L14").Value = Worksheets("Line Update").Range("E17") - Worksheets("Line Update").Range("B17")

End If
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
If you only want every other row, you can add a Step clause:

VBA Code:
For i= 11 to 18 Step 2
 
Upvote 0
Rory, I rewrote this like this and it is only giving me one calculation which by the way is incorrect. It should be 14 in the top box but it is 10.

Sub DoMath()

Dim WS As Worksheet
Dim i As Long, j As Long


Set WS = Sheets("Line Update")

For i = 11 To 18 Step 2
For j = 11 To 14
If Range("B" & i) <> Range("E" & i) Then
Range("L" & j).Value = Range("E" & i) - Range("B" & i)

End If

Next j
1666800838520.png

Next i
 

Attachments

  • 1666800758873.png
    1666800758873.png
    18.8 KB · Views: 5
Upvote 0
It's 10 because you loop the same rows for j for every iteration of i. That means that the same final calculation (169-159) is put into L11:L14, overwriting all the previous calculations.

I suspect you only wanted one loop, like this:

VBA Code:
For i = 0 to 3
If ws.cells(11+ (i * 2), "B").value <> ws.cells(11+ (i * 2), "E").Value then
ws.cells(11 + i, "L").Value = ws.cells(11+ (i * 2), "E").Value - ws.cells(11+ (i * 2), "B").Value
End If
next i
 
Upvote 0
Solution
Perfect, I have to remove the .Value in the second row of code and then it works perfectly Thank you
 
Upvote 0

Forum statistics

Threads
1,216,579
Messages
6,131,537
Members
449,654
Latest member
andz

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