本文介绍了当ByRef是带有工作表引用的范围时,用括号括起来的ByRef对ByVal的工作方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

="a ,显然,VBA函数参数中的括号将ByRefs转换为ByVals,

Apparently, parentheses in VBA function arguments turn ByRefs to ByVals,

范围复制"(其唯一可用的变量是目标范围")是否需要将该目标作为"ByRef"?

And Range Copy, whose only available variable is a Destination Range, needs that destination to be a ByRef?

那么,这如何工作?用圆括号中的ByRef指定的指定工作表"所引用的单元格是否应该转换为ByVal并导致复制失败?

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"))

推荐答案

嗯,不,不完全是.

括号使参数成为要计算的 expression ,然后传递其 value .

The parentheses make the parameter into an expression that gets evaluated and then its value is passed on.

当您调用具有两个参数的Sub时,您将看到此消息:

You will see this when you call a Sub that has two parameters:

mySub 1, 2       ' this is OK
mySub (1, 2)     ' this makes VB complain

第二个示例从(1,2)生成表达式;但是,该表达式是无效,因此VB会抱怨.

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:

myFunction 1, 2        ' this is OK
myFunction (1, 2)      ' this is an error
i = myFunction (1, 2)  ' here the parentheses are required.

这篇关于当ByRef是带有工作表引用的范围时,用括号括起来的ByRef对ByVal的工作方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!