我应该在VB / VBA中使用Call关键字吗? [英] Should I use Call keyword in VB/VBA?

查看:956
本文介绍了我应该在VB / VBA中使用Call关键字吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在VB / VBA中调用子程序时,我使用 Call 关键字。我知道它是可选的,但是使用它还是保留它更好?我一直认为它更明确,但是也许只是噪音。



此外,我在另一个论坛上也读过此书:使用 Call 关键字速度更快,因为它知道它不会返回任何值,因此它不需要设置任何堆栈空间即可为返回值腾出空间。

解决方案

啊哈。我对此一直很纳闷,甚至在VBA上阅读一本两英寸厚的书时,基本上都说不要使用它,除非您想使用VBE的查找功能来轻松地在大型项目中查找呼叫。 / p>

但是我刚刚发现了另一种用法。



我们知道可以将代码行与冒号连接起来,例如:

 功能测试(模式为布尔值)
如果mode = True则x = x + 1: Exit Sub
y = y-1
End Sub

但是如果您这样做在行首处使用过程调用的情况下,VBE假定您正在引用标签并删除所有缩进,将行与左边缘对齐(即使过程按预期调用):

  Function Test()
Function1:Function2
End Function

使用 Call 语句可以在维护时串联过程调用g您的代码缩进:

  Function Test()
调用函数1:调用函数2
结束函数

如果在上例中不使用 Call 语句,则VBE将假定 Function1是一个标签,并在代码窗口中将其左对齐,即使它不会引起错误。


I use the Call keyword when calling subs in VB/VBA. I know it's optional, but is it better to use it or leave it off? I've always thought it was more explicit, but maybe it's just noise.

Also, I read this on another forum: Using the Call keyword is faster because it knows that it is not going to return any values, so it doesn't need to set up any stackspace to make room for the return value.

解决方案

Ah ha. I have long wondered about this and even reading a two inch thick book on VBA basically says don't use it unless you want to use the Find feature of the VBE to easily find calls in large projects.

But I just found another use.

We know that it's possible to concatenate lines of code with the colon character, for example:

Function Test(mode as Boolean) 
    if mode = True then x = x + 1 : Exit Sub
    y = y - 1
End Sub

But if you do this with procedure calls at the beginning of a line, the VBE assumes that you're referring to a label and removes any indents, aligning the line to the left margin (even though the procedure is called as intended):

Function Test()
Function1 : Function2
End Function

Using the Call statement allows concatenation of procedure calls while maintaining your code indents:

Function Test()
    Call Function1 : Call Function2
End Function

If you don't use the Call statement in the above example, the VBE will assume that "Function1" is an label and left align it in the code window, even though it won't cause an error.

这篇关于我应该在VB / VBA中使用Call关键字吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆