View Single Post
  #7   Report Post  
Posted to microsoft.public.excel.newusers
JE McGimpsey JE McGimpsey is offline
external usenet poster
 
Posts: 4,624
Default Help defining a user-defined function

Guess it's at least partly a personal preference.

I tend to like to make things more explicit, rather than use defaults.

In this case, using the default ByRef won't make much of a difference,
since the UDF isn't actually trying to make assignments to any of the
arguments. It is a bit more efficient to pass ByRef (see the "Passing
Arguments Efficiently" topic in Help), but I don't think in this case
the tens of nanoseconds and 20 extra bytes on the stack will be missed.

When called from the worksheet, declaring a ByRef argument still won't
allow you to change the underlying object of the reference, so it makes
no practical difference. The exception would be if the function *did*
try to change the object, e.g.:

Public Function foo(ByRef bar as Range) As Double
bar.Value = bar.Value + 1
End Function

which will cause the value returned to the calling =foo(A1) cell to be
#VALUE!

In this case, to me, passing ByVal makes it explicit, six months down
the road when I want to update the function, that I shouldn't try to
reassign the passed arguments, nor should I expect the passed arguments
to be affected even when called from a VBA subroutine.

In article ,
"Bernard Liengme" wrote:

Would you please comment on the use of ByVal
I understand what it means but is it required/recommended?