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
[Table="width:, class:head"][tr=bgcolor:#E0E0F0][th] [/th][th]
F
[/th][/tr]
[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
9
[/td][td=bgcolor:#548235]
Quantity
[/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
10
[/td][td]
130​
[/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
11
[/td][td]
570​
[/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
12
[/td][td]
130​
[/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
13
[/td][td]
760​
[/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
14
[/td][td]
930​
[/td][/tr]
[/table]
[Table="width:, class:grid"][tr][td]Sheet: ForNext[/td][/tr][/table]
 
Last edited:

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
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,224,817
Messages
6,181,149
Members
453,021
Latest member
Justyna P

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