Why does assignment needed to be on LEFT-hand side?

mark91345

Board Regular
Joined
Feb 11, 2011
Messages
113
I am starting to learn VBA. On this For/Next loop, I am stumped by one line of code:
If myValue > 400 Then Range("F" & i).Value = myValue + 10

I get the first part: If myValue > 400 Then

but I do not understand the second part: Range("F" & i).Value = myValue + 10


Why must I use Range("F" & i).Value = on the left-hand side and the variable on the right?

I have tried
If myValue > 400 Then myValue = myValue + 10
but it does not work (nothing changes). Why doesn't it work?
If myValue = 560, then I would translate this to:
If 560 > 400 Then 560 = 560 + 10
But nothing changes on screen; yet, if I use
If myValue > 400 Then Range("F" & i).Value = myValue + 10
it works fine.

Why?




Here's the code:

Sub Simple_For_Next()Dim i As Long
Dim LastRow As Long
Dim myValue As Double
Const StartRow As Byte = 10
LastRow = Range("A" & StartRow).End(xlDown).Row

For i = StartRow To LastRow
myValue = Range("F" & i).Value

If myValue > 400 Then Range("F" & i).Value = myValue + 10

Next i
End Sub


Excel 2016 (Windows) 64 bit
F
9
Quantity
10
130​
11
570​
12
130​
13
760​
14
930​
Sheet: ForNext
 
Last edited:

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
So, i is a variable for the row number.
When i gets to the row number, if the value in that cell is greater than 400 it adds 10 to it.
But what you are trying to do is add 10 to the number, but it doesn't know where to put it....so the logic is saying put the value plus 10 in Range ("F" & i)
 
Upvote 0
Firstly, if you run this
Code:
Sub Simple_For_Next()
Dim i As Long
Dim LastRow As Long
Dim myValue As Double
Const StartRow As Byte = 10
LastRow = Range("A" & StartRow).End(xlDown).Row

For i = StartRow To LastRow
   myValue = Range("F" & i).Value

   If myValue > 400 Then
      myValue = myValue + 10
      MsgBox myValue
   End If
Next i
End Sub
You will see that it does work & myvalue increases by 10.

If you are trying to change the value in the cell you could use
Code:
Sub Simple_For_Next()
Dim i As Long
Dim LastRow As Long
Dim myValue As [COLOR=#ff0000]Range[/COLOR]
Const StartRow As Byte = 10
LastRow = Range("A" & StartRow).End(xlDown).Row

For i = StartRow To LastRow
   [COLOR=#ff0000]Set [/COLOR]myValue = Range("F" & i)

   If myValue > 400 Then myValue = myValue + 10
     
Next i
End Sub
Or simpler
Code:
Sub Simple_For_Next()
Dim i As Long
Dim LastRow As Long
Const StartRow As Byte = 10
LastRow = Range("A" & StartRow).End(xlDown).Row

For i = StartRow To LastRow
   With Range("F" & i)

      If .Value > 400 Then .Value = .Value + 10
   End With
Next i
End Sub
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,213,497
Messages
6,113,999
Members
448,543
Latest member
MartinLarkin

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