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

mark91345

Board Regular
Joined
Feb 11, 2011
Messages
110
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:

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
18,188
Office Version
2013
Platform
Windows
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)
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,270
Office Version
365
Platform
Windows
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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,270
Office Version
365
Platform
Windows
Glad we could help & thanks for the feedback
 

Forum statistics

Threads
1,082,119
Messages
5,363,268
Members
400,723
Latest member
Jsdk

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top