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]
[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
[/table][Table="width:, class:grid"][tr][td]Sheet: ForNext[/td][/tr][/table]
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: