Home > front end >  difference between two methods of assigning a variable Excel VBA
difference between two methods of assigning a variable Excel VBA

Time:09-21

I am pretty new to vba and I don't get why these two statement don't return the same result:

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

actually, the first now does its job very well that is to add 10 to the value in Range "F" but the second one just shows it adds 10 to myvalue in "Locals" Window but not in the Range F. There is no error whatsoever picked by VBA! Thank you for your advice

CodePudding user response:

The difference between your two statements is where you are assigning the value. In the first, you are telling Excel to set the value of the range to myValue 10. In the second, you are telling Excel to set the value of myValue to myValue 10. myValue is a variable declared in your VBA. It is not a cell in your spreadsheet.

  • Related