MatthewNYC
New Member
- Joined
- Feb 19, 2017
- Messages
- 18
Hi,
I need some clarity on whether it makes more sense to pass objects such as worksheets and ranges ByVal or ByRef.
When it comes to simple data types the answer is clear. If you change the value of a variable in the called procedure and want that change to be passed back to the CALLER then you use ByRef. If you do not want a value change passed back to the CALLER, you use ByVal.
However, the decision becomes harder with objects such as worksheets, userforms, and ranges.
For example, whether you pass objects ByRef or ByVal any changes made to the properties of the object are always passed back to the CALLER.
The only difference is when you pass an object such as a worksheet ByVal the called procedure cannot change the pointer to that object.. This seems to offer some protection against the called procedure accidentally changing the pointer to an object.
For example, the called procedure cannot set the ByVal worksheet object to point to a different worksheet object or even set it to Nothing.
So, would you choose ByVal or ByRef in the following scenarios:
////////////////////////////////////////////////////////////////////////////////////////////////////////
Private Sub Example1(ws As Excel.Worksheet)
... Change values to a range on the worksheet ....
End Sub
What's confusing to me is in Example 1 a range on the worksheet is changed. However, the actual worksheet itself is not changed. For example, the worksheet properties such as its name or visibility were not changed.
However, the range is contained on the worksheet so perhaps the worksheet really did "change". In this case is ByRef warranted?
////////////////////////////////////////////////////////////////////////////////////////////////////////
Private Sub Example2(rng As Excel.Range)
... Change cell styling of the change ....
End Sub
In the above case the cell styling of a range is changed. However, the actual values in the rng itself did not change. Again, this is confusing whether the rng is really changing or not and should be passed ByVal or ByRef.
////////////////////////////////////////////////////////////////////////////////////////////////////////
Does anyone have guidance on what the best practice is to follow?
Matthew
I need some clarity on whether it makes more sense to pass objects such as worksheets and ranges ByVal or ByRef.
When it comes to simple data types the answer is clear. If you change the value of a variable in the called procedure and want that change to be passed back to the CALLER then you use ByRef. If you do not want a value change passed back to the CALLER, you use ByVal.
However, the decision becomes harder with objects such as worksheets, userforms, and ranges.
For example, whether you pass objects ByRef or ByVal any changes made to the properties of the object are always passed back to the CALLER.
The only difference is when you pass an object such as a worksheet ByVal the called procedure cannot change the pointer to that object.. This seems to offer some protection against the called procedure accidentally changing the pointer to an object.
For example, the called procedure cannot set the ByVal worksheet object to point to a different worksheet object or even set it to Nothing.
So, would you choose ByVal or ByRef in the following scenarios:
////////////////////////////////////////////////////////////////////////////////////////////////////////
Private Sub Example1(ws As Excel.Worksheet)
... Change values to a range on the worksheet ....
End Sub
What's confusing to me is in Example 1 a range on the worksheet is changed. However, the actual worksheet itself is not changed. For example, the worksheet properties such as its name or visibility were not changed.
However, the range is contained on the worksheet so perhaps the worksheet really did "change". In this case is ByRef warranted?
////////////////////////////////////////////////////////////////////////////////////////////////////////
Private Sub Example2(rng As Excel.Range)
... Change cell styling of the change ....
End Sub
In the above case the cell styling of a range is changed. However, the actual values in the rng itself did not change. Again, this is confusing whether the rng is really changing or not and should be passed ByVal or ByRef.
////////////////////////////////////////////////////////////////////////////////////////////////////////
Does anyone have guidance on what the best practice is to follow?
Matthew