Apparently, parentheses in VBA function arguments turn ByRefs to ByVals,
And Range Copy, whose only available variable is a Destination Range, needs that destination to be a ByRef?
So, how is this working? Shouldn't the Cell referenced with Worksheet Specified, being a ByRef in parentheses, be converted into a ByVal and cause the Copy to fail?
Range("A1").Copy (Worksheets("Sheet1").Range("E5"))
A couple of things:
Copy doesn't care how you pass the destination
Range
, as long as it's aRange
object. So, for example:Range("A1").Copy ByVal Range("B1")
will work just fine.
Range
object properly when you add theWorksheets()
call. This has something to do with late binding as best I can tell because that Worksheets call returns anObject
type, not aWorksheet
. If you were to use a worksheet code name (e.g.Sheet1.Range("A1")
) instead, you'd have the same problem as you had originally. Really need someone like Mathieu to explain the inner workings of that.Well, no, not exactly.
The parentheses make the parameter into an expression that gets evaluated and then its value is passed on.
You will see this when you call a Sub that has two parameters:
The second example makes from
(1, 2)
an expression; however, the expression is invalid so VB will complain.If you call a function without using the return value, then do not use parentheses: