I am trying to gain some ground on VBA
I know this is pretty basic but in a Worksheet Event such as Change
Private Sub Worksheet_Change(ByVal Target as Range)
Target.Font.ColorIndex = 5
End Sub
I 've searched what ByVal means in this link
And I cannot get out a meaning out of this explanation in the help
Specifies that an argument is passed in such a way that the called procedure or property cannot change the value of a variable underlying the argument in the calling code.
Witch really confused me...
I also searched for it in various sources and found this different explanation which plunged me deeper into the darkness...it goes as:
If you don’t want the called procedure to modify any variables passed as arguments, you can modify the called procedure’s argument list so that arguments are passed to it by value rather than by reference. To do so, precede the argument with the ByVal keyword. This technique causes the called routine to work with a copy of the passed variable’s data — not the data itself.
What does he mean "that arguments are passed to it by value rather than by reference"? Can you explain me in a simple way what ByVal does, i constantly use it when i code and I want to know every bit of code i am writing.
Here is an extremely good website with examples that explains the difference between by value, and by reference: http://www.physics.nyu.edu/grierlab/idl_html_help/procedures12.html Let me know if it helps! If not, I can explain it in more depths, but I think the website adequately covers the topic.
EDIT:
By value means that it is set. You cannot call this value again in another function. By reference means that this value can be called again. So in the example, PRO ADD, A, B A = A + B RETURN END
A is passed by reference, as the user calls the A again, and it is returned. However, B is called by value. In the following example, ADD, A, 4 works. A is defined as A = A + B. So the user calls A again, and adds B to it. However, ADD, 4, A doesn't work since 4 is a value. so 4 = A + B would produce an error. Of course, this can be applied to VBA as well.
Just to add, the reason why this distinction matters is from 2 main standpoints:
Protection: In choosing between the two passing mechanisms, the most important criterion is protecting the variable from unwated changes. When calling ByRef, the procedure can return a value to the calling code through that argument. In other words, it can reference the same variable again. ByVal protects the variable from being changed by the procedure. So for example, if you want the variable to = 4 no matter what, you would use ByVal.
Performance: The passing mechanism can affect performance, but it is usually insignificant. One exception to this is a value type passed ByVal. In this case, Visual Basic copies the entire data contents of the argument. Therefore, for a large value type such as a structure, it can be more efficient to pass it ByRef.
Sorry for the long answer, hope it helps.